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)
添加回答
舉報