(24)Python實現遞歸生成或者刪除一個文件目錄及文件
阿新 • • 發佈:2017-08-17
path 生成 std fun created elif ror spa else
- import os,errno
- #基本工具類
- #①遞歸生成輸入的路徑下面的文件夾或文件
- #②遞歸刪除輸入的路徑下面的文件夾及文件
- ‘‘‘
- param : dirPath
- return :
- AuthorCreated by Wu Yongcong 2017-8-17
- function:remove a input dirPath and the files/dictionary under it
- ‘‘‘
- def removeDir(dirPath):
- if not os.path.isdir(dirPath):
- return
- files = os.listdir(dirPath)
- try:
- for file in files:
- filePath = os.path.join(dirPath, file)
- if os.path.isfile(filePath):
- os.remove(filePath)
- elif os.path.isdir(filePath):
- removeDir(filePath)
- os.rmdir(dirPath)
- except Exception as e:
- print(e)
- ‘‘‘
- param: dirPath
- Created by Wu Yongcong 2017-8-17
- function:add a input dirPath and the files/dictionary under it
- ‘‘‘
- def mkdir_p(dirPath):
- try:
- os.makedirs(dirPath)
- except OSError as oe:
- if oe.errno == errno.EEXIST and os.path.isdir(dirPath):
- pass
- else:
- raise
(24)Python實現遞歸生成或者刪除一個文件目錄及文件