2 回答
TA贡献1803条经验 获得超6个赞
要创建多个文件夹,请创建文件夹名称列表并设置父文件夹的路径:
import os
# define the name of the directory to be created
path = r"D:\test"
lst_folders = [r"Folder_A", r"Folder_B", r"Folder_C"]
for new_folder in lst_folders:
print(new_folder)
path_new = os.path.join(path, new_folder)
try:
os.makedirs(path_new)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
TA贡献1840条经验 获得超5个赞
也试试这个
import os
path = "parent"
for new in range(5):
print(f'{path}/child_{new}')
try:
os.makedirs(f'{path}/child_{new}')
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s" % path)
添加回答
举报