python - 如何使用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)
解决方案
尝试使用 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)
推荐阅读
- php - PHP 到 Typescript 的转换
- javascript - 移动浏览器/视图中的按钮“点击”动画滞后
- python - 如何为两个分类变量的每个变体打印直方图
- python - 如何使用带有 tkinter 的 tk.filedialog 保存 csv 文件?
- subdomain - 为什么从导航栏单击论坛链接时子域重定向会导致错误消息?
- python - 烧瓶中的测试和开发路线/数据库
- python - 获取错误即时消息 Django“异常值:名称'order_id'未定义”查询以列出子记录
- sql - 无法将字符串转换为时间戳
- node.js - 文件上传失败
- javascript - 为什么 React Bootstrap Card 没有在浏览器中显示?尽管使用了组件,但页面为空