1

获取驱动器的子目录(包括其中的文件)的最佳方法是什么?最好os.listdir()通过检查文件中是否有目录来使用和过滤目录'.'

任何想法都会有所帮助,我更希望我只使用标准库来完成这项任务。

4

1 回答 1

4

看一下os.walk(),它允许您访问每个目录并获取您访问的每个目录的文件列表和子目录列表。

以下是您只能下降一个级别的方法:

for root, dirs, files in os.walk(path):
    # do whatever you want to with dirs and files
    if root != path:
        # one level down, modify dirs in place so we don't go any deeper
        del dirs[:]
于 2012-05-30T16:54:58.190 回答