changing current dir and executing a shell script

Albert Hopkins marduk at letterboxes.org
Fri May 27 18:19:22 EDT 2011


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





More information about the Python-list mailing list