簡體   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)

當我運行此代碼時,即使我有權訪問該目錄,它也會給我權限錯誤。

另外,我使用 Windows 並嘗試以管理員身份運行

檢查文件夾是否不是只讀的。

或者你也可以運行命令:

import os
os.system('whoami')

它將顯示哪個用戶當前注冊為啟動用戶

嘗試使用 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