Changing a shell's current directory with python

Mike Meyer mwm at mired.org
Sun Dec 18 22:32:53 EST 2005


Peter Hansen <peter at engcorp.com> writes:
> Andy B. wrote:
>> I've got a python utility that I want to change my shell's current
>> directory based on criteria it finds.  I've scoured google and the
>> python cookbook and can't seem to figure out if this is even possible.
>>  So far, all my attempts have changed the current python session only.
>>  Am I going to have to wrap this in a shell script?
> As you've heard, you can't get there from here.  In fact, even just
> wrapping with a shell script likely won't be enough, unless you are
> willing to "source" the script every time you run it.  The only way I
> know of (and I'd be happy to hear alternatives) to do this in a
> _transparent_ manner is to combine an alias (which uses "source" for
> you) with a wrapper script _and_ to have that wrapper script read from
> some place (stdout or a temporary file or ?) to which the Python
> script can communicate the desired new environment variables and/or
> current directory.

The (slightly cleaner) alias approach is to have the alias eval the
results of invoking your python script, and have the python print the
appropriate cd command to standard output.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list