Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to get initial absolute working dir reliably?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


no.email at please

Aug 18, 2012, 8:19 AM

Post #1 of 3 (102 views)
Permalink
How to get initial absolute working dir reliably?

What's the most reliable way for "module code" to determine the
absolute path of the working directory at the start of execution?

(By "module code" I mean code that lives in a file that is not
meant to be run as a script, but rather it is meant to be loaded
as the result of some import statement. In other words, "module
code" is code that must operate under the assumption that it can
be loaded at any time after the start of execution.)

Functions like os.path.abspath produce wrong results if the working
directory is changed, e.g. through os.chdir, so it is not terribly
reliable for determining the initial working directory.

Basically, I'm looking for a read-only variable (or variables)
initialized by Python at the start of execution, and from which
the initial working directory may be read or computed.


Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


jason.swails at gmail

Aug 18, 2012, 1:53 PM

Post #2 of 3 (95 views)
Permalink
Re: How to get initial absolute working dir reliably? [In reply to]

On Sat, Aug 18, 2012 at 11:19 AM, kj <no.email [at] please> wrote:

>
> Basically, I'm looking for a read-only variable (or variables)
> initialized by Python at the start of execution, and from which
> the initial working directory may be read or computed.
>

This will work for Linux and Mac OS X (and maybe Cygwin, but unlikely for
native Windows): try the PWD environment variable.

>>> import os
>>> os.getcwd()
'/Users/swails'
>>> os.getenv('PWD')
'/Users/swails'
>>> os.chdir('..')
>>> os.getcwd()
'/Users'
>>> os.getenv('PWD')
'/Users/swails'

Of course this environment variable can still be messed with, but there
isn't much reason to do so generally (if I'm mistaken here, someone please
correct me).

Hopefully this is of some help,
Jason


wuwei23 at gmail

Aug 19, 2012, 4:58 PM

Post #3 of 3 (92 views)
Permalink
Re: How to get initial absolute working dir reliably? [In reply to]

On Sunday, 19 August 2012 01:19:59 UTC+10, kj wrote:
> What's the most reliable way for "module code" to determine the
> absolute path of the working directory at the start of execution?

Here's some very simple code that relies on the singleton nature of modules that might be enough for your needs:

import os

_workingdir = None

def set():
global _workingdir
_workingdir = os.getcwd()

def get():
return _workingdir

At the start of your application, import workingdir and do a workingdir.set(). Then when you need to retrieve it, import it again and use workingdir.get():

a.py:
import workingdir
workingdir.set()

b.py:
import workingdir
print workingdir.get()

test.py:
import a
import b

You could also remove the need to call the .set() by implicitly assigning on the first import:

if '_workingdir' not in locals():
_workingdir = os.getcwd()

But I like the explicitness.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.