我有一个由50多个其他文件夹组成的文件夹。我需要os.chdir检查第一个文件夹,退后一步,检查文件夹#2,退后一步,检查文件夹#3等等......
到目前为止,我的代码只检查硬编码文件夹,但我不知道如何自动执行它,以便检查序列中的每个文件夹。
facility_types = ["4x Clinic", "4x Hospital", "4x Lab"]
for mnemonic in os.listdir():
print (mnemonic)
print (os.listdir(mnemonic))
individual_facility = os.listdir(mnemonic)
for facility in facility_types:
if individual_facility not in os.listdir(mnemonic):
os.chdir("C:/Users/mf050034/Desktop/test/Client 1")
os.makedirs(facility)
else:
print ("All Facility Types Already Exits.")
continue
for facility in facility_types:
if individual_facility not in os.listdir(mnemonic):
os.chdir(+ 1)
os.makedirs(facility)
else:
print ("All Facility Types Already Exits.")
break
答案 0 :(得分:2)
看起来您正在尝试查看当前目录中的每个目录(设施),如果它没有来自facility_types的项目,请将其创建为目录。
import os
facility_types = ["4x Clinic", "4x Hospital", "4x Lab"]
for facility in os.listdir():
for facility_type in facility_types:
if facility_type not in os.listdir(facility):
print("Adding Facility Type '{}' to {}".format(
facility_type, facility))
os.makedirs('{}/{}'.format(facility, facility_type))