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
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ThePrinceofPython
  • 174
  • 1
  • 1
  • 9

2 Answers2

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.
Aaron
  • 2,383
  • 3
  • 22
  • 53
  • 4
    Use an administrator command prompt and then run the script again. – Aaron Feb 15 '15 at 16:19
  • 1
    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) – Minh Tran Mar 30 '17 at 13:05
  • @Minh Tran I guess you could make a batch script to runas administrator. That'd be pretty easy – chevydog Sep 15 '17 at 02:11
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 :)

  • 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. – Tim Ogilvy Nov 10 '18 at 12:06