How to find Windows "Application data" directory??

P

Paul Rubin

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?

Thanks.
 
T

Trent Mick

[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
 
R

Rune Strand

You have the environment variable APPDATA. You can access it with
os.environ().
 
P

Paul Rubin

Rune Strand said:
You have the environment variable APPDATA. You can access it with
os.environ().

Thanks!!!!!! Wow, I'd been hacking away at much messier approaches
than that. It's actually os.environ['APPDATA'] ;-)
 
P

pyguy2

I had a post yesterday on just that. Anyways, I always love it when
what can be a really annoying problem, reduces into as something simple
and elegant like a python dict. (in general, I find dictionaries
rock).

I remember a similar eureka, when some time ago I found it really neat
that split w/no args works on whitespace words. Or, things like min and
sort actually travel down levels of data structures for you. Or, when
one realizes things like "in" works on all sorts of sequences even
filehandes, or you can treat gzipped files just like normal files, or
coolness of cStringIO, or startswith and endsmith methods on strings,
or . . .

Hmm, I wonder if there is a page of the little python coolnesses. I
recall one of python annoyances.

john
 
T

Trent Mick

[Paul Rubin wrote]
Rune Strand said:
You have the environment variable APPDATA. You can access it with
os.environ().

Thanks!!!!!! Wow, I'd been hacking away at much messier approaches
than that. It's actually os.environ['APPDATA'] ;-)

Note that the APPDATA environment variable is only there on *some* of
the Windows flavours. It is there on Win2k and WinXP. It is not there on
WinME. Dunno about Win95, Win98, WinNT... but you may not care about
those old guys.

Cheers,
Trent
 
R

Renato Ramonda

Trent Mick ha scritto:
Note that the APPDATA environment variable is only there on *some* of
the Windows flavours. It is there on Win2k and WinXP. It is not there on
WinME. Dunno about Win95, Win98, WinNT... but you may not care about
those old guys.

That's (I guess) because the DOS spawn (win9x family) was single user
and did not really have concepts like home directories, profiles,
separate settings for each user... hell, it did not even have users! :-D
 
B

Bengt Richter

Rune Strand said:
You have the environment variable APPDATA. You can access it with
os.environ().

Thanks!!!!!! Wow, I'd been hacking away at much messier approaches
than that. It's actually os.environ['APPDATA'] ;-)

Hm, which windows is that? Not NT4 ;-)

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top