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


Groups > comp.lang.python > #6433 > unrolled thread

changing current dir and executing a shell script

Started bysuresh <suresh.amritapuri@gmail.com>
First post2011-05-27 14:25 -0700
Last post2011-05-28 17:52 -0400
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#6433 — changing current dir and executing a shell script

Fromsuresh <suresh.amritapuri@gmail.com>
Date2011-05-27 14:25 -0700
Subjectchanging current dir and executing a shell script
Message-ID<c49d698b-183f-46a2-8367-c7db0761ba19@glegroupsg2000goo.googlegroups.com>
Hi,
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')

Any suggestions?
thanks
suresh

[toc] | [next] | [standalone]


#6436

FromThorsten Kampe <thorsten@thorstenkampe.de>
Date2011-05-28 00:18 +0200
Message-ID<MPG.284a41bcc404f1f7989819@news.individual.de>
In reply to#6433
* suresh (Fri, 27 May 2011 14:25:52 -0700 (PDT))
> 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')

os.chdir?

[toc] | [prev] | [next] | [standalone]


#6437

FromAlbert Hopkins <marduk@letterboxes.org>
Date2011-05-27 18:19 -0400
Message-ID<mailman.2178.1306534772.9059.python-list@python.org>
In reply to#6433
On Fri, 2011-05-27 at 14:25 -0700, suresh wrote:
> Hi,
> 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 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")

-a

[toc] | [prev] | [next] | [standalone]


#6458

FromPeter Otten <__peter__@web.de>
Date2011-05-28 09:41 +0200
Message-ID<irq904$233$1@solani.org>
In reply to#6437
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")

[toc] | [prev] | [next] | [standalone]


#6492

FromAlbert Hopkins <marduk@letterboxes.org>
Date2011-05-28 17:52 -0400
Message-ID<mailman.2204.1306619578.9059.python-list@python.org>
In reply to#6458
On Sat, 2011-05-28 at 09:41 +0200, Peter Otten wrote:
> > 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: 

This is exactly what I said, but using different words.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web