How to find Windows "Application data" directory??

Trent Mick trentm at ActiveState.com
Tue Jun 28 22:10:08 EDT 2005


[Paul Rubin wrote]
> I'm writing a Windows program that needs to store some user files.
> 
> The logical place to store them is in "Application Data", right?
> 
> Is there a good way to find the correct location of that directory,
> preferably without any C extensions?  It's ok if the directory is
> found at installation time rather than runtime, and bdist_wininst does
> have a way to find it from a post-installation script.  The trouble is
> that the post-installation script doesn't seem to have an obvious way
> to communicate the info to the application for later use!
> 
> Any suggestions?

The canonical way is using the Window API SHGetFolderPath function with
the CSIDL_APPDATA key:

    from win32com.shell import shellcon, shell
    print shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0)

This requires the PyWin32 extensions (which you already have if you have
ActivePython installed).

Alternatively you could write your own little C extension to call this
Window API function... but you didn't want to do that. You can also call
this WIndows API function (in the shell32.dll) with the Python "ctypes"
modules out there.


Trent

-- 
Trent Mick
TrentM at ActiveState.com



More information about the Python-list mailing list