Change cwd in the underlying shell from Python script?

Steve Holden sholden at cox.rr.com
Tue Apr 24 12:10:28 EDT 2001


<ben at co.and.co> wrote in message
news:%mcF6.18949$ii.2113952 at afrodite.telenet-ops.be...
> Joakim Hove <hove at phys.ntnu.no> wrote:
> > bash:/original/path$ myscript.py
> >
> >   [The Python script goes through a "serious calculation", and finds
> >    the new path "/new/path/to/where/I/want/tobe".
> >
> >    When the script is finished the cwd of the underlying shell has
> >    been changed to this new path.]
> >
> > bash:/new/path/to/where/I/want/tobe$
> >
> > Is something like this possible?
>
> No. The cwd is a property of a process. You can change directories
> anywhere you want in your Python script, but it won't influence the
> parent process (bash).
>
> Greetings,
> --
> ben . de . rydt at pandora . be ------------------ your comments
> http://users.pandora.be/bdr/ ------- inl. IPv6, Linux en Pandora
>
However, what you *could* do is:

In the parent process, issue the command:

PATH=$PATH:`pathcalculator.py`; export PATH

If your pathcalculator.py produces, on standard output, the directory (or
colon-separated list of directories) you want to add to your path, the
backquotes in the shell use that value in the PATH=... assignment. If you
want to completely reset the path, you would just use

PATH=`yourpathcalculator.py`; export PATH

You don't need to bother with the export if you don't care whether
sub-shells see the new path or not, but I suspect this isn't likely to be
the case.

regards
 Steve





More information about the Python-list mailing list