Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24615

Re: how can I implement "cd" like shell in Python?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <wustcsvstudio@vip.qq.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
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; 'thu,': 0.17; 'variables': 0.17; 'shell': 0.18; 'equivalent': 0.20; 'python?': 0.20; 'skip:" 40': 0.20; 'assuming': 0.22; 'runs': 0.22; '&gt;': 0.23; 'cc:2**1': 0.24; 'linux': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'run': 0.28; 'subject:like': 0.29; 'whitespace': 0.29; 'wrap': 0.29; 'date:': 0.29; 'probably': 0.29; 'this.': 0.29; "i'm": 0.29; 'usually': 0.30; 'e.g.': 0.30; 'function': 0.30; 'implement': 0.32; 'skip:- 10': 0.32; 'everyone.': 0.33; 'skip:- 20': 0.34; 'or,': 0.34; 'path': 0.35; 'subject:?': 0.35; 'something': 0.35; 'subject:': 0.36; 'subject:" ': 0.36; 'thank': 0.36; 'does': 0.37; 'from:': 0.38; 'some': 0.38; 'sure': 0.38; 'called': 0.39; 'skip:" 10': 0.40; 'help': 0.40; 'your': 0.60; 'first': 0.61; 'skip:n 10': 0.63; 'skip:$ 10': 0.66; 'to:charset:iso-8859-1': 0.75; '"can\'t"': 0.84; 'execution.': 0.84; 'message-id:@qq.com': 0.84; 'received:64.71': 0.84; 'received:64.71.138': 0.84; 'received:qq.com': 0.84; 'x-mailer:qqmail 2.x': 0.84; 'this;': 0.91
X-QQ-SSF 0000000000000060000000000000000
X-HAS-ATTACH no
X-QQ-BUSINESS-ORIGIN 2
X-Originating-IP 110.96.238.163
X-QQ-STYLE
X-QQ-mid webmail569t1340894014t24317
From "Alex chen" <wustcsvstudio@vip.qq.com>
To "Evan Driscoll" <driscoll@cs.wisc.edu>
Subject Re: how can I implement "cd" like shell in Python?
Mime-Version 1.0
Content-Type multipart/alternative; boundary="----=_NextPart_4FEC6B3E_DD517470_3A6B6426"
Content-Transfer-Encoding 8Bit
Date Thu, 28 Jun 2012 22:33:34 +0800
X-Priority 3
X-QQ-MIME TCMime 1.0 by Tencent
X-Mailer QQMail 2.x
X-QQ-Mailer QQMail 2.x
X-QQ-ReplyHash 424759570
Cc python-list <python-list@python.org>, d <d@davea.name>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1607.1340894056.4697.python-list@python.org> (permalink)
Lines 90
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340894056 news.xs4all.nl 6985 [2001:888:2000:d::a6]:33470
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24615

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

OK,I see!

Thank you everyone.

 
 




------------------ Original ------------------
From:  "Evan Driscoll"<driscoll@cs.wisc.edu>;
Date:  Thu, Jun 28, 2012 10:27 PM
To:  "Alex chen"<wustcsvstudio@vip.qq.com>; 
Cc:  "d"<d@davea.name>; "python-list"<python-list@python.org>; 
Subject:  Re:   how can I implement "cd" like shell in Python?



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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re:   how can I implement "cd" like shell in Python? "Alex chen" <wustcsvstudio@vip.qq.com> - 2012-06-28 22:33 +0800

csiph-web