我试图弄清楚如何在 python 中编写一个脚本,其中我用“ls”列出当前目录的内容,然后将 ls 的结果放入一个列表中,以便我可以对 ls 中的每个元素执行一些操作。实际上,我首先尝试在终端中执行此操作,然后使用 python 编写脚本,但我什至无法让它在终端中工作。
哦,我尝试将 ls 的输出重定向到文件中,但我似乎没有这样做的权限:
>>> subprocess.call(["ls ", directory_expanded, " > ", "namesoutput.txt"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/subprocess.py", line 478, in call
p = Popen(*popenargs, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
最佳答案
怎么样os.listdir
?
In [25]: os.listdir('.')
Out[25]: ['blah']
关于python - 列表中的目录内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744068/