亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 python 中一次為 Windows 創建多個文件夾

如何在 python 中一次為 Windows 創建多個文件夾

蕭十郎 2023-05-23 16:16:23
嘿,我是編程新手,誰能幫我指明正確的方向?我正在嘗試為一個排序程序將多個文件夾放在一個文件夾中,幾乎是在檢查文件夾是否存在,以及它是否不以文件類型命名的文件夾進行排序。我在查找它并找到了一些代碼,但我不明白它正在寫入計算機的哪個位置。這是我在網上找到的代碼:import os# define the name of the directory to be createdpath = "/tmp/year/month/week/day"try:    os.makedirs(path)except OSError:    print ("Creation of the directory %s failed" % path)else:    print ("Successfully created the directory %s" % path)
查看完整描述

2 回答

?
慕碼人8056858

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)


查看完整回答
反對 回復 2023-05-23
?
慕斯709654

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)


查看完整回答
反對 回復 2023-05-23
  • 2 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號