我必须遍历多个文件夹,最后检查最终文件夹中是否有三个文件夹。 如果文件夹中存在三个文件夹,则不执行任何操作,否则打印文件夹路径或名称。我发现很难使其工作, 欢迎任何建议,提前致谢
这就是我现在所拥有的,
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')
答案 0 :(得分:2)
os.walk已经为您提供了获取文件夹中所有文件的功能:
check_empty = set(check_empty) # set is faster for simple check in unique items
for root, dirs, files in os.walk('/mnt/sdc1/Mahajan/'):
for file in files:
filename = os.path.basename(file)
if filename in check_empty:
print(root + 'Good')
else:
print(root + 'NotGood')