python - 如果文件夹中复制了许多文件和文件夹,则在 Windows 上删除和创建 Python 文件夹将面临权限错误
问题描述
我有以下 python 脚本从 git bash shell 在 Windows 上运行。
import os
import shutil
shutil.rmtree('myfolder', ignore_errors=True)
os.mkdir('myfolder')
如您所见,它只是创建一个文件夹并删除一个文件夹。它运作良好。但是,如果我将一堆文件夹和文件复制到“myfolder”中,就会遇到麻烦。如果文件夹加载了许多文件和文件夹,它会返回以下错误。
$ python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
os.mkdir('myfolder')
PermissionError: [WinError 5] Access is denied: 'myfolder'
上述问题仅发生在 Windows 上,而不发生在 macos 上。我做错什么了吗?有没有比shutil.rmtree
and更好的方法os.mkdir
?
解决方案
推荐阅读
- go - 如何将 javascript 热重载集成到带有 https 的 golang 路由中以进行开发?
- excel - 用一些vba代码写,死路一条!得到错误结束块结束如果没有结束如果
- android - 创建没有停止点的旋转动画
- sql-server - Python 3.X micro ORM 兼容 SQL Server
- java - Android - 根据方向设置属性
- python - 按钮功能
- git - 合并到开发分支会导致功能分支被删除
- ios - UITableViewCell 子视图显示在单元格之外
- kubernetes - 带有 Hashicorp Vault 的 GKE - 可以使用 Google Cloud Run 吗?
- python-3.x - Python 3 和 Selenium - 如何组合多个 For 循环 - 抓取 Web 表