2

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'

1 Answers1

3

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.

Steve
  • 7,171
  • 2
  • 30
  • 52
  • so this is a directory I guess? `SpecialFolder.ApplicationData path =C:\Users\patricka\AppData\Roaming SpecialFolder.MyDocuments path =\\zaw\Mydocs\patricka\My Documents` –  Apr 30 '16 at 14:35
  • Yes, they're directories. It looks like your `MyDocuments` are stored on a network drive though. In Python, you should use the environment variable rather than the `System.Environment.SpecialFolder` you're quoting from. – Steve Apr 30 '16 at 14:40
  • can you show me please complete python example with your code, how you show directory folder for each user, I need this folder for one text document with data. I've tried, but have error `SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 48-49: truncated \UXXXXXXXX escape ` maybe I'm doing something wrong, windows 10 –  Apr 30 '16 at 15:21
  • The code I've put in the answer works in Python 2.7 and 3.5. It looks like you've got something wrong with your code. I suspect you've got a `\U` somewhere. E.g. `C:\Users`. The `\U` is a Unicode escape sequence. See [this answer](http://stackoverflow.com/questions/1347791/unicode-error-unicodeescape-codec-cant-decode-bytes-cannot-open-text-file) – Steve Apr 30 '16 at 17:29
  • The `os.getenv('PROGRAMDATA')` has the drive information in it. You just need to specify `my_new_folder` and not `C:\my_new_folder`. I've updated the answer to put a print statement in to show where the folder is created. Try it in your code so you can understand what is going on. – Steve Apr 30 '16 at 20:06
  • You don't have to use `%APPDATA%`. It's just another environment variable you can use to get a different base folder location. – Steve Apr 30 '16 at 20:20
  • The exact folders you should store files depends on what they're for. Try searching for advice or asking a new question on SO. – Steve Apr 30 '16 at 21:06
  • new question maybe you can help here too http://stackoverflow.com/questions/36961119/how-to-create-folder-for-each-user –  Apr 30 '16 at 22:58
  • If you're creating a directory in `%PROGRAMDATA%`, you almost always want to modify the ACL to give all users the right to modify and delete files in the directory. Otherwise the default is that only the user that created the directory (and also administrators) has the right to modify and delete files. As the owner when creating it, you of course have the right to modify the DACL, e.g. to grant all users full control: `subprocess.call(r'icacls.exe "%s" /grant Users:(CI)(OI)(F)' % mypath)`. – Eryk Sun Jun 19 '16 at 14:41