I would like my application to store some data for access by all users. Using Python, how can I find where the data should go?
Take a look at http://ginstrom.com/code/winpaths.html. This is a simple module that will retrieve Windows folder information. The module implements get_common_appdata
to get the App Data folder for all users.
Previous answer removed due to incompatibility with non-US versions of Windows, and Vista.
EDIT: To expand on Out Into Space's answer, you would use the
winpaths.get_common_appdata
function. You can get winpaths using easy_install winpaths
or by going to the pypi page, http://pypi.python.org/pypi/winpaths/, and downloading the .exe installer.
If you don't want to add a dependency for a third-party module like winpaths, I would recommend using the environment variables already available in Windows:
Specifically you probably want ALLUSERSPROFILE
to get the location of the common user profile folder, which is where the Application Data directory resides.
e.g.:
C:\> python -c "import os; print os.environ['ALLUSERSPROFILE']"
C:\Documents and Settings\All Users
EDIT: Looking at the winpaths module, it's using ctypes so if you wanted to just use the relevant part of the code without installing winpath, you can use this (obviously some error checking omitted for brevity).
import ctypes
from ctypes import wintypes, windll
CSIDL_COMMON_APPDATA = 35
_SHGetFolderPath = windll.shell32.SHGetFolderPathW
_SHGetFolderPath.argtypes = [wintypes.HWND,
ctypes.c_int,
wintypes.HANDLE,
wintypes.DWORD, wintypes.LPCWSTR]
path_buf = wintypes.create_unicode_buffer(wintypes.MAX_PATH)
result = _SHGetFolderPath(0, CSIDL_COMMON_APPDATA, 0, 0, path_buf)
print path_buf.value
Example run:
C:\> python get_common_appdata.py
C:\Documents and Settings\All Users\Application Data
You can access all of your OS environment variables using the os.environ
dictionary in the os
module. Choosing which key to use from that dictionary could be tricky, though. In particular, you should remain aware of internationalized (i.e., non-English) versions of Windows when using these paths.
os.environ['ALLUSERSPROFILE']
should give you the root directory for all users on the computer, but after that be careful not to hard code subdirectory names like "Application Data," because these directories don't exist on non-English versions of Windows. For that matter, you may want to do some research on what versions of Windows you can expect to have the ALLUSERSPROFILE environment variable set (I don't know myself -- it may be universal).
My XP machine here has a COMMONAPPDATA environment variable which points to the All Users\Application Data folder, but my Win2K3 system does not have this environment variable.
FROM http://snipplr.com/view.php?codeview&id=7354
homedir = os.path.expanduser('~')
# ...works on at least windows and linux.
# In windows it points to the user's folder
# (the one directly under Documents and Settings, not My Documents)
# In windows, you can choose to care about local versus roaming profiles.
# You can fetch the current user's through PyWin32.
#
# For example, to ask for the roaming 'Application Data' directory:
# (CSIDL_APPDATA asks for the roaming, CSIDL_LOCAL_APPDATA for the local one)
# (See microsoft references for further CSIDL constants)
try:
from win32com.shell import shellcon, shell
homedir = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)
except ImportError: # quick semi-nasty fallback for non-windows/win32com case
homedir = os.path.expanduser("~")
To get the app-data directory for all users, rather than the current user, just use shellcon.CSIDL_COMMON_APPDATA
instead of shellcon.CSIDL_APPDATA
.