changing current dir and executing a shell script

S

suresh

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
 
T

Thorsten Kampe

* 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?
 
A

Albert Hopkins

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.:

-a
 
P

Peter Otten

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:
shell=True)
/usr/share
/usr/lib

But I agree with you that
 
A

Albert Hopkins

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top