Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Changing a shell's current directory with python (http://www.velocityreviews.com/forums/t352624-changing-a-shells-current-directory-with-python.html)

Andy B. 12-18-2005 11:53 PM

Changing a shell's current directory with 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?

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

Many thanks,

-A

Lawrence Oluyede 12-19-2005 12:08 AM

Re: Changing a shell's current directory with python
 
irIl 2005-12-18, Andy B. <abuecker@gmail.com> 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"

Dennis Lee Bieber 12-19-2005 12:12 AM

Re: Changing a shell's current directory with python
 
On Sun, 18 Dec 2005 15:53:11 -0800, "Andy B." <abuecker@gmail.com>
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.
--
> ================================================== ============ <
> wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> wulfraed@dm.net | Bestiaria Support Staff <
> ================================================== ============ <
> Home Page: <http://www.dm.net/~wulfraed/> <
> Overflow Page: <http://wlfraed.home.netcom.com/> <


Andy B. 12-19-2005 12:22 AM

Re: Changing a shell's current directory with python
 
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 <wlfraed@ix.netcom.com> wrote:
> On Sun, 18 Dec 2005 15:53:11 -0800, "Andy B." <abuecker@gmail.com>
> 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.
> --
> > ================================================== ============ <
> > wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> > wulfraed@dm.net | 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
>


firebush 01-30-2011 05:16 AM

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


All times are GMT. The time now is 01:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.