bash 使用python重现bash命令'ls -a'输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20590704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use python to reproduce bash command 'ls -a' output
提问by APZ
I am new to python and am working on writing bash ls
command in python, I am stuck on ls -a
option which (according to the manpage):
我是 python 的新手,正在ls
用 python编写 bash命令,我坚持使用以下ls -a
选项(根据手册页):
Include directory entries whose names begin with a dot (`.')
包括名称以点 (`.') 开头的目录条目
I am aware of os.listdir() but it does not list special entries '.' and '..'
我知道 os.listdir() 但它没有列出特殊条目 '.' 和 '..'
From the docs: os.listdir(path):
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
来自文档:os.listdir(path):
返回一个包含路径给定目录中条目名称的列表。该列表的顺序是任意的。它不包括特殊条目“.” 和 '..' 即使它们存在于目录中。
I need help in listing these special entries through python, I would appreciate if someone can help me out here a little.
我需要通过 python 列出这些特殊条目的帮助,如果有人能在这里帮助我一点,我将不胜感激。
Thanks all for your patience.
感谢大家的耐心等待。
采纳答案by Martijn Pieters
Just add them manually to os.listdir()
result. result = [os.curdir, os.pardir] + os.listdir(path)
.
只需手动添加它们即可os.listdir()
。result = [os.curdir, os.pardir] + os.listdir(path)
.
Most modern filesystems no longer create the actual hardlinks but all APIs include the names explicitly anyway.
大多数现代文件系统不再创建实际的硬链接,但无论如何所有 API 都明确包含名称。