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

M

Marco Aschwanden

I want to store a file in the application directory. What is the easiest
way to figure out, where my application is situated?

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

Thanks for any hint,
Marco
 
G

Grant Edwards

I want to store a file in the application directory.

1) Don't

2) Why?
What is the easiest way to figure out, where my application is situated?

3) Under Unix in general there is no reliable, portable way to
do that.
sys.argv[0] does not work for my purposes... do you have any
other ideas.

Yes.

It sounds like you're trying to do a bad thing in the Unix
world. Firstly, some smart admins have entire filesystems
mounted read-only. Your application may live in one of those.

Secondly, global configuration files go in /etc. Per-user
configuration files go in the user's home directory in
..<appname>-conf .<appname>rc or under the .<appname>/
directory. If you want them preserved across reboots, then
caches of stuff go in /var/cache/<appname>. Files that don't
need to survive a reboot go in /tmp.

What are you trying to do?
 
M

Marco Aschwanden

I want to store a file in the application directory.
1) Don't

2) Why?

- I want to store the current state of the application when leaving the
application in an ini-file.
- It is a "single-user" pc!
- It is Windows.
- I don't like (others would say hate) to use the registry.
- I prefer having ini-files in the application directory.

3) Under Unix in general there is no reliable, portable way to
do that.
sys.argv[0] does not work for my purposes... do you have any
other ideas.

Yes.

It sounds like you're trying to do a bad thing in the Unix
world. Firstly, some smart admins have entire filesystems
mounted read-only. Your application may live in one of those.

Secondly, global configuration files go in /etc. Per-user
configuration files go in the user's home directory in
.<appname>-conf .<appname>rc or under the .<appname>/
directory. If you want them preserved across reboots, then
caches of stuff go in /var/cache/<appname>. Files that don't
need to survive a reboot go in /tmp.

What are you trying to do?

As I said: It is a windows machine, single user, small app... but I am
very glad you made this remarks, cause it reminds me to consider
portability when writing apps - and now I can see, where I am failing.
Thanks.

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:

sys.users_home_dir

Is there anything like it in Python?

Thanks,
Marco
 
G

Grant Edwards

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

I could tell you the right thing to do under Unix. In Windows,
I'm lost. I sort of assumed you were writing for a Unix
system, since your question is asked constantly in various
newsgroups and it's almost always being asked by a Win32
programmer working on his first Unix application.
 
R

Roger Binns

Marco said:
- 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
 
M

Michael Geary

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

CSIDL_PERSONAL is the user's "My Documents" folder, which is not the right
place for configuration files. It's for documents that the user explicitly
creates and saves.

Configuration files generally belong in a subdirectory under CSIDL_APPDATA.
Large data files that shouldn't be uploaded and downloaded when roaming
profiles are used belong in a subdirectory under CSIDL_LOCAL_APPDATA. On
systems that don't support CSIDL_LOCAL_APPDATA, fall back to CSIDL_APPDATA
instead.

-Mike
 
R

Roger Binns

Michael said:
CSIDL_PERSONAL is the user's "My Documents" folder, which is not the right
place for configuration files. It's for documents that the user explicitly
creates and saves.

That is why I pointed to the list of CSIDL constants since the OP will
need to use their judgement as to where to store stuff based on what
it actually is. One should also take into account that if the user
is using a roaming profile then some paths returned will be local and
some will be remote.

Roger
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top