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

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

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


P: n/a
On 2004-04-28, Marco Aschwanden <PP**********@spammotel.com> wrote:
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?

--
Grant Edwards grante Yow! After THIS, let's go
at to PHILADELPHIA and have
visi.com TRIPLETS!!
Jul 18 '05 #2

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

Jul 18 '05 #3

P: n/a
On 2004-04-28, Marco Aschwanden <PP**********@spammotel.com> 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!
- 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.

--
Grant Edwards grante Yow! I just had a MAJOR
at CONTRACT DISPUTE with
visi.com SUZANNE SOMERS!!
Jul 18 '05 #4

P: n/a
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
Jul 18 '05 #5

P: n/a
Roger Binns wrote:
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
Jul 18 '05 #6

P: n/a
Michael Geary wrote:
Roger Binns wrote:
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.


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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.