2 回答

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
要?jiǎng)?chuàng)建多個(gè)文件夾,請創(chuàng)建文件夾名稱列表并設(shè)置父文件夾的路徑:
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貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
也試試這個(gè)
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)
添加回答
舉報(bào)