python批量建立資料夾

2022-03-19 06:27:09 字數 1278 閱讀 8965

參考資料

python:os.path.join()產生的斜槓在windows和linux下的不同表現和解決方法

python獲取指定目錄下所有檔名os.walk和os.listdir

使用os.listdir()獲取路徑下所有檔名

判斷是否是資料夾有多種判斷方式,我使用if (("." in top_files) == false)檔名中是否有點號

os.path.exists判斷是否存在,如果已經存在就不建立了,不存在就建立

os.makedirs 新增資料夾

import os

# 判斷資料夾中是否有這些資料夾,沒有的話就建立它

def initialize(file_dir):

for al_name in os.listdir(file_dir):

if (("." in al_name) == false):

if not os.path.exists(os.path.join(file_dir, al_name).replace('\\', '/')):

# print(os.path.join(file_dir, al_name).replace('\\', '/'))

os.makedirs(os.path.join(file_dir, al_name).replace('\\', '/'))

# 如果其中沒有"1"的資料夾

if not os.path.exists(os.path.join(file_dir, al_name, "1").replace('\\', '/')):

os.makedirs(os.path.join(file_dir, al_name, "1").replace('\\', '/'))

# 如果其中沒有"2"的資料夾

python批量建立資料夾

有時需要建立一些同型別的資料夾儲存階段性的檔案,可以py指令碼實現批量建立資料夾 首先建立資料夾有兩個api 建立單層資料夾 import os path dir example ifnot os.path.exists path os.mkdir path 遞迴建立資料夾 import os pa...

批量建立資料夾

import os 匯入模組 path test 設定建立後資料夾存放的位置 for i in range 30 41 這裡建立10個資料夾 定義乙個變數判斷檔案是否存在,path指代路徑,str i 指代資料夾的名字 k 03d i i ists os.path.exists path str k...

windows批量建立資料夾

後處理的時候需要建立非常多的資料夾用以存放資料,而且資料夾的名字是有規律的。手動建立的話非常慢而且繁瑣,於是就搜尋批量建立資料夾的方法。在windows中可以用bat命令實現批量建立資料夾,但首先需要得到資料夾的名字。這可以用excel來實現。比如,我要建立的資料夾的名字只包含數字,從641到670...