Changing a shell's current directory with python

Discussion in 'Python' started by Andy B., Dec 18, 2005.

  1. Andy B.

    Andy B. Guest

    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?

    % pwd
    /var/tmp
    % myutil.py
    # do some stuff and cd to '/var/log'
    % pwd
    /var/log

    Many thanks,

    -A
    Andy B., Dec 18, 2005
    #1
    1. Advertising

  2. irIl 2005-12-18, Andy B. <> ha scritto:
    > 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?
    >
    > % pwd
    > /var/tmp
    > % myutil.py
    > # do some stuff and cd to '/var/log'
    > % pwd
    > /var/log


    Just look in the library:

    import os
    os.chdir("path")


    --
    Lawrence - http://www.oluyede.org/blog
    "Anyone can freely use whatever he wants but the light at the end
    of the tunnel for most of his problems is Python"
    Lawrence Oluyede, Dec 19, 2005
    #2
    1. Advertising

  3. On Sun, 18 Dec 2005 15:53:11 -0800, "Andy B." <>
    declaimed the following in comp.lang.python:

    > 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?


    That's about all you will be able to achieve... the inheritance goes
    downwards: shell -> program(python, etc.) -> spawned processes
    (os.system, etc.)... Changes at one level are only picked up by things
    started after that change, and started from that level.
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Dec 19, 2005
    #3
  4. Andy B.

    Andy B. Guest

    Many thanks for the sanity check. Just wanted to check with the gurus
    before heading down another path.

    -A


    On 12/18/05, Dennis Lee Bieber <> wrote:
    > On Sun, 18 Dec 2005 15:53:11 -0800, "Andy B." <>
    > declaimed the following in comp.lang.python:
    >
    > > 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?

    >
    > That's about all you will be able to achieve... the inheritance goes
    > downwards: shell -> program(python, etc.) -> spawned processes
    > (os.system, etc.)... Changes at one level are only picked up by things
    > started after that change, and started from that level.
    > --
    > > ============================================================== <
    > > | Wulfraed Dennis Lee Bieber KD6MOG <
    > > | Bestiaria Support Staff <
    > > ============================================================== <
    > > Home Page: <http://www.dm.net/~wulfraed/> <
    > > Overflow Page: <http://wlfraed.home.netcom.com/> <

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Andy B., Dec 19, 2005
    #4
  5. Andy B.

    firebush

    Joined:
    Jan 30, 2011
    Messages:
    1
    You can use alias to change your directory

    I'm not sure whether this is helpful, but here's a common thing I do to change my directory using a python script:

    1) Create a python script that does stuff and then prints a directory name.
    2) Use an alias that calls that script and cd's to the output.

    Here's an example that creates a directory in pwd from today's date and changes into it:

    $ cd /tmp
    $ cat > /tmp/test.py
    #!/usr/bin/env python

    import datetime
    import os

    today = datetime.date.today()
    dir_string = today.strftime("%Y%m%d")
    os.mkdir(dir_string)
    print dir_string
    $ chmod 755 /tmp/test.py
    $ alias cdtoday='cd `/tmp/test.py`'
    $ cdtoday
    $ pwd
    /tmp/20110129
    firebush, Jan 30, 2011
    #5
    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. Peter Hansen
    Replies:
    1
    Views:
    318
    Mike Meyer
    Dec 19, 2005
  2. Dave Rudolf
    Replies:
    2
    Views:
    365
    Daniel Pitts
    Jan 17, 2007
  3. vsoler

    Changing the current directory

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

Share This Page