Persistent Directory Change?

Steve Holden sholden at holdenweb.com
Fri May 31 09:50:24 EDT 2002


"Dave Butler" <davebutlerREMOVE at hotmail.com> wrote ...
> I have a need for the current directory from a Python script to persist
> after the script ends. Is this possible? A simple approach such as
>
> --------------------
> import os
>
> print os.getcwd()
> os.chdir(r'f:\temp')
> print os.getcwd()
> --------------------
>
> does not work. It seems the directory change occurs in the Python process,
> but not in the command prompt which called it.
>
> I am using Python 2.1 under Windows 2000.
>

Under Unix things are arranged so that subshells (such as the one used to
run a Python program) get their own copy of their parent shell's
environment. Thus changes are explicitly forbidden from propagating to
parent shells unless you use scripting in the parent shell (such as

    cd `python yourprog.py`

for example, if you can arrange for your program to write the required
directory out as its only output). Under Windows things are a lot sloppier,
but not very consistent. I'd suggest you look at your requirements a little
more critically to see what laternatives there might be.

Sorry to be less than positive.

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list