【发布时间】:2018-01-05 13:34:13
【问题描述】:
我必须遍历多个文件夹,最后检查最终文件夹中是否有三个文件夹。 如果文件夹中存在三个文件夹,则什么也不做,否则打印文件夹路径或名称。我发现很难让它工作, 欢迎任何建议,提前谢谢
这是我目前拥有的,
check_empty =['One','Two','Three']
path = '/mnt/sdc1/Username/'
folders = next(os.walk('/mnt/sdc1/Mahajan/'))[1]
for folder in folders:
files_in_folders = os.listdir(path +folder)
for files_in in files_in_folders:
for files in os.listdir(path+folder+'/'+files_in):
for items in check_empty:
if files in check_empty:
print(folder +'Good')
else:
print(folder+'NotGood')
【问题讨论】:
-
对于这个特定的用例我可能是错的,但我通过经验和讲座了解到,通常,当你在 python 中有超过 2 个嵌套循环时,你做错了。
-
那么,如果我从一张白纸开始,我该如何处理呢?
-
好的,谢谢。
标签: python