<p>OK,I see!</p><p>Thank you everyone.</p><p> </p><div> </div><div><includetail><div><br></div><div><br></div><div style="padding: 2px 0px; font-family: Arial Narrow; font-size: 12px;">------------------ Original ------------------</div><div style="background: rgb(239, 239, 239); padding: 8px; font-size: 12px;"><div><b>From: </b> "Evan Driscoll"<driscoll@cs.wisc.edu>;</div><div><b>Date: </b> Thu, Jun 28, 2012 10:27 PM</div><div><b>To: </b> "Alex chen"<wustcsvstudio@vip.qq.com>; <wbr></div><div><b>Cc: </b> "d"<d@davea.name>; "python-list"<python-list@python.org>; <wbr></div><div><b>Subject: </b> Re:   how can I implement "cd" like shell in Python?</div></div><div><br></div>On 6/28/2012 7:28, Alex chen wrote:<br>> I just want to write a python program,it can be called in the linux<br>> terminal like the command "cd" to change the directory of the shell terminal<br><br>You "can't" do this; a program the shell runs cannot affect the shell's<br>execution.<br><br>What you have to do is have some help from the shell. Have your Python<br>program output the path to the directory you want to change to. Then you<br>can run it as follows<br>   cd $(new-directory.py)<br>or, if has arguments,<br>   cd $(new-directory.py foo blah)<br><br>(The $(...) is usually spelled as `...` around the internet. If you're<br>unfamiliar, what it does is run the command then substitute the *output*<br>of that command at the command line.)<br><br><br>Eventually you probably want to wrap this up so you don't have to do<br>that every time. You can use a shell function for this. Assuming you're<br>using an 'sh' derivative, it will look something like<br>   function my-cd() {<br>      cd $(new-directory.py "$@")<br>   }<br><br><br>I'm not a shell programmer and I always forget the names of the<br>variables holding the arguments, so check that at first and make sure<br>it's passing the right thing to the new-directory script, e.g. that it<br>works with whitespace in the arguments and that it isn't including the<br>equivalent to argv[0] in the script.<br><br>Evan<br><br></includetail></div>