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: References: 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 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")