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


Groups > comp.lang.python > #6458

Re: changing current dir and executing a shell script

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: changing current dir and executing a shell script
Followup-To comp.lang.python
Date Sat, 28 May 2011 09:41:55 +0200
Organization None
Lines 33
Message-ID <irq904$233$1@solani.org> (permalink)
References <c49d698b-183f-46a2-8367-c7db0761ba19@glegroupsg2000goo.googlegroups.com> <mailman.2178.1306534772.9059.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace solani.org 1306568516 2147 eJwFwYEBACAEBMCVvvA0jsj+I3RnwsVypVFtbNLvTlo8PxAZB3DP210NogRZ7Apnj96l4R8YNBD6 (28 May 2011 07:41:56 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Sat, 28 May 2011 07:41:56 +0000 (UTC)
X-User-ID eJwFwYEBwDAEBMCVEF6NE/r2HyF3caCYdAQ8NnYNyist17K73VHTXj93KDi5t2a21GlFJtdGQvEhG0G1B3cUFic=
Cancel-Lock sha1:8xCgZnaaZEBtSspuXKs6AWkce0Y=
X-NNTP-Posting-Host eJwFwQkBwDAIA0BLfIFNDoXGv4TewVNzKhIZIGh/dZl29SWSypkdOy1HKscMsa3mK18j1LfCm9tYccegyITErQMdF+ED4fUZ+g==
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6458

Followups directed to: comp.lang.python

Show key headers only | View raw


Albert Hopkins wrote:

> On Fri, 2011-05-27 at 14:25 -0700, suresh wrote:

>> I want to execute the following command line stuff from inside python.
>> $cd directory
>> $./executable
>> 
>> I tried the following but I get errors
>> import subprocess
>> subprocess.check_call('cd dir_name;./executable')
>> 
>> Due to filename path issues, I cannot try this version.
>> subprocess.check_call('./dir_name/executable')

> You don't want to do this because "cd" is a built-in shell command, and
> subprocess does not execute within a shell (by default).

The problem is not that cd is built-in, but that there is no shell at all. 
You can change that with shell=True:

>>> subprocess.check_call("cd /usr/share; pwd; cd /usr/lib; pwd", 
shell=True)
/usr/share
/usr/lib
 
But I agree with you that 

> The proper way to do this is to use the "cwd" keyword argument to
> subprocess calls, i.e.:
> 
>>>> subprocess.check_call(('/path/to/exec',), cwd="/path/to/dir")

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


Thread

changing current dir and executing a shell script suresh <suresh.amritapuri@gmail.com> - 2011-05-27 14:25 -0700
  Re: changing current dir and executing a shell script Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-05-28 00:18 +0200
  Re: changing current dir and executing a shell script Albert Hopkins <marduk@letterboxes.org> - 2011-05-27 18:19 -0400
    Re: changing current dir and executing a shell script Peter Otten <__peter__@web.de> - 2011-05-28 09:41 +0200
      Re: changing current dir and executing a shell script Albert Hopkins <marduk@letterboxes.org> - 2011-05-28 17:52 -0400

csiph-web