简体   繁体   English

PermissionError: [WinError 5] 访问在 Python 中被拒绝

[英]PermissionError: [WinError 5] Access is denied in Python

import shutil

def create_dir(path):
    if not os.path.exists(path):
        os.mkdir(path)
    else:
        shutil.rmtree(path)

when I run this code it gives me permission error, even though I have access to that directory.当我运行此代码时,即使我有权访问该目录,它也会给我权限错误。

Also, I use Windows and I have tried running as administrator另外,我使用 Windows 并尝试以管理员身份运行

Check if the folder is not Read-Only.检查文件夹是否不是只读的。

Or you can also run command:或者你也可以运行命令:

import os
os.system('whoami')

It will show you which user is currently registered as launching user .它将显示哪个用户当前注册为启动用户

Try with ignore_errors=True,尝试使用 ignore_errors=True,

import shutil

def create_dir(path):
    if not os.path.exists(path):
        os.mkdir(path)
    else:
        shutil.rmtree(path, ignore_errors = True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM