views:

4861

answers:

5

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?

+6  A: 

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.

Robert S.
+1 This looks very useful, thanks, but Jays answer gets me what I want with no added dependency
David Sykes
A: 

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.

tgray
Unfortunately, that will not hold up on non-English OS's.
Jeff
Could you be more specific, please?
tgray
I believe he means that "Documents and Settings" is localized in non-English copies of XP. Also, it is possible to change the location of this folder via the registry. Finally, Vista names this folder "Users" instead. Lesson: don't hard-code it.
Ben Blank
But using the winpaths.get_common_appdata uses the registry value, correct? This method should be cross-lingual.
tgray
I'd suggest removing the first part of the answer, hard coding this value would be wrong for vista or any non english XP
David Sykes
I've updated the content above to remove the wrong part.
tgray
+10  A: 

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
Jay
What if the user is on a non-English version of Windows? Does ALLUSERSPROFILE still apply?
Robert S.
@Out Into Space - it does at least on the Italian version of windows, not sure about other non-English versions.
Jay
Cool, good to know!
Robert S.
+1  A: 

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.

Jeff
+2  A: 

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.

Pykler
OP asked for data accessible by all users
Jason S