Python 建立資料夾

2021-10-06 19:03:04 字數 578 閱讀 2282

def

mkdir

(path)

:# 引入模組

import os

# 去除首位空格

path = path.strip(

)# 去除尾部 \ 符號

path = path.rstrip(

"\\"

)# 判斷路徑是否存在

# 存在 true

# 不存在 false

i***ists = os.path.exists(path)

# 判斷結果

ifnot i***ists:

# 如果不存在則建立目錄

# 建立目錄操作函式

os.makedirs(path)

print

(path +

' 建立成功'

)return

true

else

:# 如果目錄存在則不建立,並提示目錄已存在

print

(path +

' 目錄已存在'

)return

false

Python建立資料夾

import os 批量建立資料夾,用來存放按日儲存的日常工作檔案 def mkdir all path 獲取檔案是否存在 folder os.path.exists all path ifnot folder os.makedirs 建立檔案時如果路徑不存在會建立這個路徑 os.makedirs ...

建立資料夾

當某資料夾不存在時,建立資料夾 import os path dir file if not os.path.exists path dir os.makedirs path dir 同時建立資料夾有兩種函式,os.mkdir和os.makedirs,兩者的區別在於前者是一級一級建立檔案目錄,後者可...

python批量建立資料夾

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