Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'cache': 0.05; 'context': 0.05; 'installed.': 0.05; 'linux,': 0.05; 'say,': 0.05; 'parameter': 0.07; 'advice.': 0.09; 'before.': 0.09; 'os.getcwd()': 0.09; 'os.path': 0.09; 'path.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'files.': 0.13; 'do,': 0.15; 'between.': 0.16; 'cwd': 0.16; 'different,': 0.16; 'directory...': 0.16; 'md5': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'situation.': 0.16; 'variable.': 0.16; 'wrote:': 0.17; 'config': 0.17; 'directory.': 0.17; 'script.': 0.17; '>>>': 0.18; 'code,': 0.18; 'windows': 0.19; 'bit': 0.21; 'import': 0.21; 'constant': 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; 'correct': 0.28; 'header:X-Complaints- To:1': 0.28; 'environment': 0.29; '>>>>': 0.29; 'faster,': 0.29; 'initialized': 0.29; 'source': 0.29; "i'm": 0.29; 'install': 0.29; 'usually': 0.30; 'relative': 0.30; 'sense': 0.31; 'gets': 0.32; 'file': 0.32; 'launch': 0.32; 'could': 0.32; 'directory,': 0.33; "he's": 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'that,': 0.34; 'thanks': 0.34; 'dir': 0.35; 'saved': 0.35; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'created': 0.36; 'except': 0.36; 'but': 0.36; 'wanted': 0.36; 'xml': 0.37; 'option': 0.37; 'two': 0.37; 'uses': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'files': 0.38; 'easier': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'decision': 0.60; 'first': 0.61; 'kind': 0.61; 'provide': 0.62; 'different': 0.63; 'within': 0.64; 'therefore': 0.65; 'prime': 0.65; 'validate': 0.65; 'lose': 0.71; 'frank': 0.75; 'calculations': 0.84; 'etc,': 0.84; 'otten': 0.84; 'subject:find': 0.84; 'tree,': 0.84; 'equipped': 0.91; 'leak': 0.91; 'subject:Best': 0.91; 'angel': 0.93; 'response,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Frank Millman Subject: Re: Best way to find starting directory Date: Tue, 19 Mar 2013 16:29:36 +0200 References: <51485E35.7090501@davea.name> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 197.87.30.250 User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <51485E35.7090501@davea.name> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363703382 news.xs4all.nl 6840 [2001:888:2000:d::a6]:60821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41508 On 19/03/2013 14:46, Dave Angel wrote: > 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. >>> [...] >> >> 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. > Thanks Dave, good advice. I learnt an important lesson from Peter's response, as I did not fully understand the difference before. Now that I do, I am better equipped to make the correct decision for a given situation. As you say, there is a variety of types of data that one might to store externally. My current scenario is that, in my business/accounting application, I use xml to store form definitions, report definitions, etc, which are kept in the database (compressed). I am now constructing some xml schemas to validate the xml files. I need to store the schemas somewhere, so I have created a directory called 'schemas' under the main directory. I need to access them from various parts of the application, so I need a reliable way to locate the 'schemas' directory. In theory I could store them somewhere different, and use a parameter to provide the path. But they are only used within the context of the application, so I think it makes sense to keep them alongside the 'py' files that make up the application. Frank