python - 列表中的目录内容

标签 python linux unix command-line io-redirection

我试图弄清楚如何在 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/

相关文章:

python - 没有名为 ConfigParser 的模块

python - 谁能解释一下我知道装饰器的功能但仍然没有得到输出

linux - 映射到给定键盘快捷键的 linux shell 命令的名称?

linux - 如果 grep 找到匹配项,则打印 grep 关键字

命令参数的 Unix 管道

c - 输出仍然保留在缓冲区中并在每个子进程之后打印出来

python - 如何在 Django 中获取登录用户 ID

python - 在 Python 3 中使用 UTF-8 解码 VIEWSTATE 字符串

linux - 剪切和 awk 命令 : Delimiter behaviour

c - 管道缓冲区大小是 4k 还是 64k?