What are the Python 3 os commands to establish "writable data files and C-Drive paths" for a typical Windows 10 Application. There are several parts to this question:
My Python 3 program creates (multiple) data files as part of it's purpose. When my MSI installer installs into /Programs, my Python executable does not have permission to create and write data files. Thus, the first part of my question is: Do I need to change my Python 3 program to create data files in a specific directory (using the os capabilities) and could you give me an example.
The second part of my question is simply: What os command options can assist me in discovering the windows 10 directories of a general Windows 10 PC (e.g. the home path, the AppData path, etc).
Note that I am cx_Freezing to an MSI installer, so everything must be automated for a typical remote install from a cloud (google drive or GitHub) by the MSI installer, so keep that in mind when answering 1 and 2 above.
Attention: Here is the MSI Installer for this program: New WINDOWS 10 Contact Management Application.
https://drive.google.com/drive/folders/0Bz98wvqqw-1QRUNFcUJLU21yT1k
Thanks in advance for your programming knowledge and experience.
I appreciate your technical assistance and clarification.
I suggest that you avoid storing files and directories directly in the user's profile folder (i.e. the
UserProfile
environment variable) or home folder (i.e."%HomeDrive%%HomePath%"
). This differs from the common practice in Unix for a home directory (if we're ignoring the XDG base-directory spec), but when in Redmond, do as the Microsofties do.Create a folder that's uniquely named for the application in one or more of the following locations: the local data folder (per-user), the roaming data folder (per-user), or the program data folder (per-machine). Note that these folders are hidden by default since generally users aren't meant to access them directly.
Use a local data folder for caches. Use a roaming data folder for stateful user data and configuration. Use a program data folder for data and caches that aren't specific to a user. For example, a program like pip could use the program data folder to cache downloaded packages. (In practice, pip caches packages per user, but in principle it could cache per machine.)
If your application uses a program data folder, make sure the folder grants all users permission to add and modify subfolders and files. If you create the folder lazily, you can add the permissions manually. See this answer for an example of how to modify file security.
The environment variables for the local, roaming, and program data folders are, respectively,
LocalAppData
,AppData
andProgramData
. In Windows XP the latter is"%AllUsersProfile%\Application Data"
, and maybe "Application Data" is localized. Generally you shouldn't use these environment variables in an application.Since most known/special folders are easily relocatable in Explorer, it's best to ask the shell for the current path by calling
SHGetFolderPath
or the newerSHGetKnownFolderPath
function instead of using environment variables and default locations. You can use ctypes for this if you need to stay within Python's standard library. But it's easier to use PyWin32, which can be pip installed as the "pypiwin32" package.Here are some Known Folder GUIDs for data, documents, and media files:
Here are the corresponding
CSIDL
constants, except there isn't one for "Downloads":SHGetKnownFolderPath
isn't wrapped by PyWin32. I have another answer that calls it via ctypes. Alternatively, you can use PyWin32 to create aKnownFolderManager
instance. For example:Or call the legacy
SHGetFolderPath
function with aCSIDL
constant. For example: