首页 > 解决方案 > 如果文件夹中复制了许多文件和文件夹,则在 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.rmtreeand更好的方法os.mkdir

标签: pythonpython-3.xwindows

解决方案


推荐阅读