473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
6 1941
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
>> 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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jerrygarciuh | last post by:
Hi all, I have been working on a GUI web photo album for *nix. I had thought it might be workable for users to open a browser based FTP session to upload pics to a newly created album...
4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
1
by: Todd Johnson | last post by:
Hey, I have been working on a program that stores information for each user in a subdirectory of the directory in which the script is run. So if my script is in /home/someuser/myprogram, the...
1
by: Pierre Rouleau | last post by:
I can't remember where/how sys.path is set (aside from the automatically loaded site.py) and i get a strange entry in it. Can anyone remind me where/how to control sys.path on a Win32 machine (i'm...
3
by: Ben Allfree | last post by:
Written to normalize web server path names based on a virtual root. I propose that something equivalent to this be added to os.path ------------------------------------- import os.path import...
9
by: Amir Dekel | last post by:
Hi everyone, I have two problems: 1. How can I keep my changes in sys.path after closing the interpreter? 2. os.path.expanduser("~") always gives me "C:\\" instead of my homepath. I have...
6
by: bukzor | last post by:
I have to go into these convulsions to get the directory that the script is in whenever I need to use relative paths. I was wondering if you guys have a better way: from os.path import dirname,...
4
by: Tim Cook | last post by:
Hi All, I just ran into an issue with the rstrip method when using it on path strings. When executing a function I have a need to strip off a portion of the current working directory and add...
8
by: tow | last post by:
I have a python script (part of a django application, if it makes any difference) which is exhibiting the following behaviour: import my_module # succeeds imp.find_module("my_module") # fails,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.