我有一个目录,其中包含许多子目录,在每个子目录中我有更多的子目录。在
我有一个python代码,它将目录和子目录打印并写入文件中。代码:
import os
file = open("list", "w")
for root, dirs, files in os.walk("./customers/"):
print root
file.write(root+"\n")
其输出为:
^{pr2}$我只想:
./customers/A1
./customers/A2
./customers/B1
./customers/B2
./customers/C1
./customers/C2
你似乎不愿意更新你的问题来明确你想要什么,所以我猜你只需要叶子目录。你可以这样做:
对于您的目录结构,应该输出:
^{pr2}$看起来可能是你想要的。在
如果要对输出进行排序,可以编写生成器函数并对其输出进行排序:
输出将:
相关问题 更多 >
编程相关推荐