Python 为什么我收到 WindowsError: [错误 5] 访问被拒绝?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28528020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:23:10  来源:igfitidea点击:

Why am i getting WindowsError: [Error 5] Access is denied?

pythonprogram-fileswindowserror

提问by ThePrinceofPython

Trying to create program that adds folders into program files-recieving this error:

尝试创建将文件夹添加到程序文件中的程序 - 收到此错误:

WindowsError: [Error 5] Access is denied 'C:\Program Files\IMP'

Here is my code

这是我的代码

import os, sys, random
numb= 1
x=True
while x==True:
    newpath = ((r'C:\Program Files\IMP\folder_%s') % (numb))
    if not os.path.exists(newpath):
        os.makedirs(newpath)
    numb=numb+1
    if numb==11:
        x=False

回答by Aaron

Because you have to have the "system administrator privileges" to create dirs under C:\Program Files.

因为您必须具有“系统管理员权限”才能在C:\Program Files.

So try run the script with system administrators privilege.

所以尝试使用系统管理员权限运行脚本。



To start a command prompt as an administrator

以管理员身份启动命令提示符

  1. Click Start.
  2. In the Start Search box, type cmd, and then press CTRL+SHIFT+ENTER.
  3. Run the python script.
  1. 单击开始。
  2. 在“开始搜索”框中,键入cmd,然后按CTRL+ SHIFT+ ENTER
  3. 运行 python 脚本。

回答by Zsigamer

Right click on file (which file/folder's permissions needed to execute the script) go properties, security and enable all permissions, little checkboxes -> ALLOW: "every application package & limited application package & trusted installer"

右键单击文件(执行脚本所需的文件/文件夹权限)转到属性,安全性并启用所有权限,小复选框-> 允许:“每个应用程序包和有限的应用程序包和受信任的安装程序”

this is it :)

就是这个 :)