繁体   English   中英

Python os.walk() 获取目录前的文件

[英]Python os.walk() get files before directories

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

从 Linux 手册页:

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

如何才能做到这一点?

您可以像这样使用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从下到上遍历目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM