By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to find Windows "Application data" directory??

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
[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
Tr****@ActiveState.com
Jul 19 '05 #2

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

Jul 19 '05 #3

P: n/a
"Rune Strand" <ru*********@gmail.com> writes:
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'] ;-)
Jul 19 '05 #4

P: n/a
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

Jul 19 '05 #5

P: n/a
[Paul Rubin wrote]
"Rune Strand" <ru*********@gmail.com> writes:
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

--
Trent Mick
Tr****@ActiveState.com
Jul 19 '05 #6

P: n/a
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

--
Renato
--------------------------------
Usi Fedora? Fai un salto da noi:
http://www.fedoraitalia.org
Jul 19 '05 #7

P: n/a
On 28 Jun 2005 21:09:12 -0700, Paul Rubin <http://ph****@NOSPAM.invalid> wrote:
"Rune Strand" <ru*********@gmail.com> writes:
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
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.