Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41490

Re: Best way to find starting directory

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'anyway.': 0.04; 'cache': 0.05; 'installed.': 0.05; 'linux,': 0.05; 'skip:/ 10': 0.07; 'skip:o 50': 0.07; 'python': 0.09; 'os.getcwd()': 0.09; 'os.path': 0.09; 'between.': 0.16; 'cwd': 0.16; 'directory...': 0.16; 'executed,': 0.16; 'md5': 0.16; 'path:': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'config': 0.17; 'script.': 0.17; '>>>': 0.18; 'code,': 0.18; 'windows': 0.19; 'bit': 0.21; 'import': 0.21; 'constant': 0.22; 'explicit': 0.22; 'stick': 0.22; "i'd": 0.22; 'somewhere': 0.24; 'second': 0.24; 'script': 0.24; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'separate': 0.27; 'environment': 0.29; 'cat': 0.29; 'faster,': 0.29; 'initialized': 0.29; 'source': 0.29; "i'm": 0.29; 'install': 0.29; 'that.': 0.30; 'usually': 0.30; 'relative': 0.30; 'gets': 0.32; 'file': 0.32; 'launch': 0.32; 'print': 0.32; 'directory,': 0.33; "he's": 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'wrong': 0.34; 'dir': 0.35; 'saved': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'option': 0.37; 'two': 0.37; 'uses': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'easier': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'your': 0.60; 'first': 0.61; 'kind': 0.61; "you'll": 0.62; 'different': 0.63; 'therefore': 0.65; 'results': 0.65; 'prime': 0.65; 'lose': 0.71; 'received:74.208': 0.71; 'frank': 0.75; 'calculations': 0.84; 'otten': 0.84; 'received:74.208.4.194': 0.84; 'subject:find': 0.84; 'tree,': 0.84; 'leak': 0.91; 'subject:Best': 0.91
Date Tue, 19 Mar 2013 08:46:45 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3
MIME-Version 1.0
To python-list@python.org
Subject Re: Best way to find starting directory
References <ki93v1$s7f$1@ger.gmane.org> <ki95lp$bel$1@ger.gmane.org> <ki975r$odu$1@ger.gmane.org>
In-Reply-To <ki975r$odu$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:UQ8eVMxBlVpgiJt15Mo88YSlXzUtopOaX9qXkf1giqR bZceEuG9vEvrqs/veXV11FYVD2opC2SkS9dgn3RXV8AvPPxn3h smzH0UjqZBzibpI9SYvq/CrtUF6buKa6QmSpKY+S5kSuqaCvJk eUWFBfXHz5bD0wBwe5Fxp7jzxcpOFpgAAO/SF/A4bVuw/Fac6y tElcElVJLc6NsvOrRsXwMmZecp2rpLz+S0B7eCKIgwpzfDqSIG 9y2EtmT28+PgcViv1PCX/KnId76q5DXtAHfKthynr6axmopU/L rdzYpjCcHqYyXE7XcP7NLzHJ61EL0B6n9fzKXAfcqhcaYeyLg= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3497.1363697225.2939.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363697225 news.xs4all.nl 6989 [2001:888:2000:d::a6]:41021
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41490

Show key headers only | View raw


On 03/19/2013 04:21 AM, Frank Millman wrote:
> On 19/03/2013 09:55, Peter Otten wrote:
>> Frank Millman wrote:
>>
>>> I want to locate a file relative to the directory from which the main
>>> program was launched.
>>>
>>> I have found two ways of finding the starting directory -
>>>
>>> 1.
>>> import os
>>> dir = os.getcwd()
>>
>> This gives the current working directory...
>>
>>> 2.
>>> import os.path
>>> import __main__
>>> dir = os.path.dirname(__main__.__file__)
>>
>> ... and this gives the location of your main script.
>>
>>> I know that the first one will return the wrong result if os.chdir() has
>>> been executed, but I don't do that.
>>
>> You'll get different results when you launch the script with an explicit
>> path:
>>
>> $ cat millman/demo.py
>> import os
>> import __main__
>>
>> print "cwd:", os.getcwd()
>> print "script path:", os.path.abspath(os.path.dirname(__main__.__file__))
>> $ python millman/demo.py
>> cwd: /home/frank
>> script path: /home/frank/millman
>>
>>
>
> That makes sense. I usually launch the script from its own directory,
> but that is not guaranteed.
>
> Therefore option 2 is the way to go.

You might want to reconsider.  There are really two different kinds of 
data files you might want to access from your script. The first is 
constant data that gets initialized when the script is installed.  And 
the second is user data that he's thinking about right now.

For example, if a script uses a saved cache of prime numbers to make 
calculations a bit faster, it might keep that file in with its own 
source code, or relative to it.

And if I wanted to calculate md5 sums for a directory tree, I'd usually 
make that my cwd before starting the script.

Config files are somewhere in between.  In Linux, get them relative to 
the $HOME environment variable.

FWIW, I try to keep all the first kind of files on a separate partition, 
and except where other programs force me, never let them leak onto the 
OS+program partition.  That way, I'm not likely to lose an important 
jpeg when the OS trashes its partition.  This particular paranoia is 
left over from my Windows days, but I stick to it anyway.  It also makes 
it easier to migrate to a new OS.  Just format the OS partition and 
install the OS and all the apps.  The data is already separate.

-- 
DaveA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Best way to find starting directory Dave Angel <davea@davea.name> - 2013-03-19 08:46 -0400

csiph-web