Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!204.52.135.9.MISMATCH!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.04; 'subject:Python': 0.05; 'arguments': 0.07; 'python': 0.09; 'arguments,': 0.09; 'script,': 0.09; 'spelled': 0.09; 'cc:addr :python-list': 0.10; 'programmer': 0.11; '(the': 0.15; 'passing': 0.15; 'substitute': 0.16; 'wrote:': 0.17; 'alex': 0.17; 'script.': 0.17; 'variables': 0.17; 'shell': 0.18; 'equivalent': 0.20; 'assuming': 0.22; 'runs': 0.22; 'cc:2**1': 0.24; 'linux': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'run': 0.28; 'subject:like': 0.29; 'whitespace': 0.29; 'wrap': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'usually': 0.30; 'e.g.': 0.30; 'function': 0.30; 'or,': 0.34; 'path': 0.35; 'received:192.168.0': 0.35; 'subject:?': 0.35; 'something': 0.35; 'subject:" ': 0.36; 'does': 0.37; 'some': 0.38; 'sure': 0.38; 'received:192': 0.39; 'called': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'first': 0.61; 'skip:n 10': 0.63; 'skip:$ 10': 0.66; 'subjectcharset:iso-8859-1': 0.67; 'subject::': 0.83; '"can\'t"': 0.84; 'execution.': 0.84; 'this;': 0.91 Date: Thu, 28 Jun 2012 09:27:05 -0500 From: Evan Driscoll User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Alex chen Subject: Re: =?ISO-8859-1?Q?=DE=1A_how_can_I_implement_=22cd=22?= =?ISO-8859-1?Q?_like_shell_in_Python=3F?= References: In-Reply-To: X-Enigmail-Version: 1.4.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Cc: python-list , d X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340894225 news.xs4all.nl 6880 [2001:888:2000:d::a6]:35478 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24616 On 6/28/2012 7:28, Alex chen wrote: > I just want to write a python program,it can be called in the linux > terminal like the command "cd" to change the directory of the shell terminal You "can't" do this; a program the shell runs cannot affect the shell's execution. What you have to do is have some help from the shell. Have your Python program output the path to the directory you want to change to. Then you can run it as follows cd $(new-directory.py) or, if has arguments, cd $(new-directory.py foo blah) (The $(...) is usually spelled as `...` around the internet. If you're unfamiliar, what it does is run the command then substitute the *output* of that command at the command line.) Eventually you probably want to wrap this up so you don't have to do that every time. You can use a shell function for this. Assuming you're using an 'sh' derivative, it will look something like function my-cd() { cd $(new-directory.py "$@") } I'm not a shell programmer and I always forget the names of the variables holding the arguments, so check that at first and make sure it's passing the right thing to the new-directory script, e.g. that it works with whitespace in the arguments and that it isn't including the equivalent to argv[0] in the script. Evan