import os
imshutil
1.刪除普通文件夾 shutil.rmtree(path,True)
2.刪除文件名稱過長致使沒法刪除的文件夾
def re_move():
path_ = "要刪除的文件夾路徑" file_list = os.listdir(path_) for file_ in file_list: new_folder = "{}\\new".format(path_) del_folder = "{}\\{}".format(path_, file_) if not os.path.exists(new_folder): os.mkdir(new_folder) os.chdir(path_) cmd_ = "robocopy {} {} /purge".format(new_folder, del_folder) os.system(cmd_) os.removedirs(del_folder) os.removedirs(new_folder)