user6185396
user6185396

Reputation:

PermissionError: [WinError 5]

I'm trying create folder:

    import os

mypath = (r'C:\Program Files\my_folder')
if not os.path.isdir(mypath):
   os.makedirs(mypath)

I got error:

mkdir(name, mode)
PermissionError: [WinError 5] Access is denied: 'C:\\Program Files\\my_folder'

Upvotes: 2

Views: 4144

Answers (1)

Steve
Steve

Reputation: 7271

The script does not have permissions to write to the Program Files folder. In Windows, this is a folder that is protected by very high level of permissions and generally should not be written to, except by installers.

Assuming you need to store data specific to the machine, use the %PROGRAMDATA% environment variable instead. Note that when accessing an environment variable in Python, do not use the % symbol.

import os
mypath = os.path.join(os.getenv('programdata'), 'my_folder')
if not os.path.isdir(mypath):
    os.makedirs(mypath)

print (mypath)

Will create the folder, and output the path:

C:\ProgramData\my_folder

If you need to store data for each user, use the %APPDATA% environment variable instead.

Upvotes: 3

Related Questions