Path ... where is my application's home dir?

Roger Binns rogerb at rogerbinns.com
Wed Apr 28 15:43:28 EDT 2004


Marco Aschwanden wrote:
> - I want to store the current state of the application when leaving the
> application in an ini-file.
> - It is a "single-user" pc!

Note that since Windows 95, you can have multiple users, but only
one at a time could be logged in.  With XP you can actually have
multiple logged in at the same time (Press Windows Key + L)

> - I don't like (others would say hate) to use the registry.

Your users are going to be used to the registry being used.
But it is your app with your rules :-)

> - I prefer having ini-files in the application directory.

Many people install the apps as administrator and then run
as a ordinary user.  They won't be able to write to that
directory.  (Note this is a VERY common configuration
in a corporate environment).

> > > sys.argv[0] does not work for my purposes... do you have any
> > > other ideas.

This works on Windows, Linux and Mac, even when the application
has been frozen using py2exe, cx_Freeze and BundleBuilder respectively:

p=sys.path[0]
if p.lower().endswith(".zip"): # py2exe zip importer in action
    p=os.path.dirname(p)
appdirectory=os.path.abspath(p)

> And yes, the best place to store this information would be in the user's
> home dir, but then: Where is it? How to find it under Windows - every
> version of windows changes the place for home dirs. It would be nice to
> have something like this in a system/version independet way:

If the system is Windows 2000 or XP then os.path.expanduser("~") will
give their home directory.  (That also works on UNIX and Mac).

If you want to do things properly on Windows, you should use the
registry, or use this code to find the right folder.  You need
win32all installed.

from win32com.shell import shell, shellcon
path=shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, None, 0)


The list of CSIDL constants is at MSDN:

 http://tinyurl.com/7hei

Roger





More information about the Python-list mailing list