473,725 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1960
On 2004-04-28, Marco Aschwanden <PP**********@s pammotel.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**********@s pammotel.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().endsw ith(".zip"): # py2exe zip importer in action
p=os.path.dirna me(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.expandu ser("~") 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.SHGe tFolderPath(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.SHGe tFolderPath(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_APP DATA. On
systems that don't support CSIDL_LOCAL_APP DATA, 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.SHGe tFolderPath(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
14789
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 directory. The problem I am having is opening the FTP session in the new folder. I.E. we have just created images/myAlbum1. Using realpath(.) we get /home/someUser/public_html/images/myAlbum1 . Unfortunately the browser based FTP wants the site root...
4
12367
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 the file myclass.class? Will this work if it's run from a relative path, like: java ../progs/myclass
1
2100
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 config files and such are in /home/someuser/myprogram/config. Now if I invoke the interpreter from the same directory as the script is in I can load and save the files because cwd points to that directory(I used cwd to get the path
1
8590
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 running XP on the one where the issue arises). I can't figure out why the 'C:\WINDOWS\System32\python23.zip' entry is inside sys.path. That file/dir does not exist on the disk. Could a failing (crashing) Pythonwin installation caused that? ...
3
1776
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 copy def vnormpath(root,path): """ Normalize a path based on a virtual root. """
9
2444
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 tried to change my homepath in WinXP using set homepath(in command line), and alsaw using the "Environment Variables" in WinXP system properties, non helped. I really have to fix this somehow.
6
4055
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, realpath, abspath here = dirname(realpath(abspath(__file__.rstrip("c")))) In particular, this takes care of the case of symlinked, compiled scripts, which is fairly common at my workplace, and I imagine in many *nix evironments.
4
1420
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 on a path to a log file. Initially this worked great but then I added a branch in SVN which caused the path to contain 'LNCCWorkshop'. The rstrip() then began removing the characters 'shop' leaving an incorrect path to the log file. When I
8
1563
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, raising ImportError which is completely baffling me. According to sys.path, both should fail; the directory containing my_module is not in sys.path (though the my_module directory itself is). More puzzlingly, printing out my_module.__file__...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.