【问题标题】:Python os.walk() get files before directoriesPython os.walk() 获取目录前的文件
【发布时间】:2022-01-05 09:32:28
【问题描述】:

我想使用 python os.walk() 模拟 bash find 命令的 -depth 参数。

来自 Linux 手册页:

-depth Process each directory's contents before the directory
              itself.

如何做到这一点?

【问题讨论】:

标签: python find depth os.walk


【解决方案1】:

您可以像这样使用filesdirs

import os
root_path = r'/home/testuser/test/'
for root, dirs, files in os.walk(root_path, topdown=True):
    print(root)
    print(dirs)
    print(files)

您也可以使用topdown=False从下到上浏览目录

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多