首页 > 解决方案 > 如何使用python真正删除文件夹中的所有文件和Windows上的文件夹?

问题描述

这不会删除文件夹

shutil.rmtree('C:\\Users\\0\\Downloads\\preoutput')

这会导致此错误 Message=[WinError 5] Access is denied: 'C:\Users\0\Downloads\testdelete'

import os
import glob

files = glob.glob('C:\\Users\\0\\Downloads\\preoutput')
for f in files:
    #os.chmod(f, 0o777)
    os.remove(f)

标签: pythonwindowsmoduledelete-fileshutil

解决方案


尝试使用 shutil.rmtree() 来实现这个程序

import shutil
import os 

#location 
location = "C:\\Users\\0\\Downloads\\preoutput"
#directory 
dir = "preoutput"
#path 
path = os.path.join(location, dir) 
#removing directory 
shutil.rmtree(path)

推荐阅读