How to get initial absolute working dir reliably?

Jason Swails jason.swails at gmail.com
Sat Aug 18 16:53:44 EDT 2012


On Sat, Aug 18, 2012 at 11:19 AM, kj <no.email at please.post> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120818/972f439a/attachment.html>


More information about the Python-list mailing list