-1

I have a basic Windows question. I am a Linux user, but I am responsible for a program that needs to operate on the Windows file system.

I am seeking a list of safe, universally available high-level directories within which any Python program will be allowed to create directories and files on any Windows machine.

The following error message is being thrown when we try to run the code at bottom.

PermissionError: [WinError 5] Access is denied: 'C:\\Program Files\\arbitrary'

The code that throws the above error is:

import os
import platform

cwd = os.getcwd()
platform.system()

print("cwd is: ", cwd)

if platform.system() == 'Windows':
  print('W!')
  drive = cwd.split(':\\')[0] + ':\\'
  print('drive is: ', drive)

newpath = drive+'Program Files\\arbitrary' 
print('newpath is: ', newpath)

if not os.path.exists(newpath):
  os.makedirs(newpath)

Clearly, our program does not have permission to write to the Program Files directory.

But instead of requiring our users to have permissions on any given directory, we need to have the program specify writing to a directory which guaranteed will be writeable for any user that can run the program.

halfer
  • 19,824
  • 17
  • 99
  • 186
CodeMed
  • 9,527
  • 70
  • 212
  • 364

2 Answers2

1

Such directories on Windows are generally referred to by well-known environment variables. It's not entirely clear from your question whether you are looking to write persistent or temporary data, and whether you would expect data written by one user to be visible and/or modifiable by another user.

If the data is temporary, then use the directory referred to by the TEMP environment variable.

Data that is private to the user would normally be written to the directories referred to by either the APPDATA (roams with the user) or LOCALAPPDATA (local to the machine) environment variables.

It's perhaps less clear where data to be shared between users should be written. One option would perhaps be somewhere within the public user directory referred to by the PUBLIC environment variable. Files written there are owned by the user that initially creates them, but the default inherited permissions allow other interactive users to modify them.

Iridium
  • 23,323
  • 6
  • 52
  • 74
  • You cannot assume anything about environment variables, sometimes they don't exist for various reasons. – Anders Feb 03 '22 at 00:47
1

Windows provides several "special" locations where files are stored.

You would generally pick one of these if you need to write to files:

  • CSIDL_APPDATA Files that roam to different computers in NT domains. Good for configuration files. Also available as the %AppData% environment variable.
  • CSIDL_LOCAL_APPDATA Files that never roam. Good for cache and other things that is not vital or can be regenerated automatically. Also available as %LocalAppData%.
  • CSIDL_PERSONAL Documents. Don't store configuration files here.
  • %Public% Folder shared by all users on a machine.
  • %Temp% The temp directory. Only available as an environment variable.

See this question for methods to retrieve these in Python.

Anders
  • 97,548
  • 12
  • 110
  • 164