Re: Changing a shell's current directory with python

Discussion in 'Python' started by Peter Hansen, Dec 19, 2005.

  1. Peter Hansen

    Peter Hansen Guest

    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.

    -Peter
    Peter Hansen, Dec 19, 2005
    #1
    1. Advertising

  2. Peter Hansen

    Mike Meyer Guest

    Peter Hansen <> 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 <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Dec 19, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy B.
    Replies:
    4
    Views:
    24,343
    firebush
    Jan 30, 2011
  2. Dave Rudolf
    Replies:
    2
    Views:
    371
    Daniel Pitts
    Jan 17, 2007
  3. vsoler

    Changing the current directory

    vsoler, Nov 15, 2009, in forum: Python
    Replies:
    0
    Views:
    359
    vsoler
    Nov 15, 2009
  4. vsoler
    Replies:
    8
    Views:
    665
    Gabriel Genellina
    Nov 16, 2009
  5. suresh
    Replies:
    4
    Views:
    468
    Albert Hopkins
    May 28, 2011
Loading...

Share This Page