python刪除整個目錄(目錄非空)

mxw8 9年前發布 | 1K 次閱讀 Python

python刪除整個目錄(目錄非空),os.rmdir()只能刪除空目錄,下面的函數可以刪除整個非空目錄

import os
import shutil

def CleanDir( Dir ): if os.path.isdir( Dir ): paths = os.listdir( Dir ) for path in paths: filePath = os.path.join( Dir, path ) if os.path.isfile( filePath ): try: os.remove( filePath ) except os.error: autoRun.exception( "remove %s error." %filePath )#引入logging elif os.path.isdir( filePath ): if filePath[-4:].lower() == ".svn".lower(): continue shutil.rmtree(filePath,True) return True

Dir = "D:\Temp" CleanDir(Dir)</pre>

 本文由用戶 mxw8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!