简体   繁体   中英

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

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,

import shutil

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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