Python徹底刪除文件夾及其子文件

#coding:utf-8
import os
import stat
import shutil
#filePath:文件夾路徑
def delete_file(filePath):
    if os.path.exists(filePath):
        for fileList in os.walk(filePath):
            for name in fileList[2]:
                os.chmod(os.path.join(fileList[0],name), stat.S_IWRITE)
                os.remove(os.path.join(fileList[0],name))
        shutil.rmtree(filePath)
        return "delete ok"
    else:
        return "no filepath"
    
print os.path.exists("E:\\biancheng\\python\\ascas")    
print delete_file("E:\\biancheng\\python\\ascas")



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章