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

2

14

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
Dovelike answered 15/2, 2015 at 16:3 Comment(1)
do you have write permissions?Ulick
A
17

Because you have to have the "system administrator privileges" to create dirs under 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.
Accost answered 15/2, 2015 at 16:11 Comment(3)
Use an administrator command prompt and then run the script again.Accost
Is there a way to setup command prompt so that a "user command prompt" always starts up with particular privileges that administrator command prompts have? (Like the privileges that allow a user command prompt to install python modules using pip)Marjie
@Minh Tran I guess you could make a batch script to runas administrator. That'd be pretty easyCosta
S
0

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 :)

Selfdriven answered 10/11, 2018 at 11:58 Comment(1)
This looks like a pretty good answer, but could be improved with explaining what you see as the cause of the problem, and why you think this will solve it.Femoral

© 2022 - 2024 — McMap. All rights reserved.