使用python重新生成bash命令'ls-a'outpu

2024-04-20 01:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,正在用python编写bash ls命令,我一直在使用ls -a选项,该选项(根据手册页):

Include directory entries whose names begin with a dot (`.')

我知道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.

我需要通过python列出这些特殊条目的帮助,如果有人能在这里帮我一点忙,我将不胜感激。

谢谢大家的耐心。


Tags: thepathin命令bashnamesos选项
1条回答
网友
1楼 · 发布于 2024-04-20 01:15:22

只需手动将它们添加到os.listdir()结果中。result = [os.curdir, os.pardir] + os.listdir(path)

大多数现代文件系统不再创建实际的硬链接,但是所有的api都显式地包含这些名称。

相关问题 更多 >