def force_delete(self, path=None):
path = os.path.abspath(path or self.path)
if path is None or os.path.exists(path) == False : return
if os.path.isfile(path):
os.chmod(path, stat.S_IWRITE)
os.remove(path)
return
try : shutil.rmtree(path)
except :
del_dir_paths = []
for dirpath, dirnames, filenames in os.walk(path):
try : shutil.rmtree(dirpath)
except :
del_dir_paths.append(dirpath)
for filename in filenames:
del_file_path = os.path.join(dirpath, filename)
os.chmod(del_file_path, stat.S_IWRITE)
os.remove(del_file_path)
del_dir_paths.sort(reverse=True)
for del_dir_path in del_dir_paths:
os.chmod(del_dir_path, stat.S_IWRITE)
os.rmdir(del_dir_path)
评论列表
文章目录