Python subprocess module


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python subprocess module
# 1  
Old 03-17-2011
Python subprocess module

I need to run this command using python subprocess module (notice I'm using only variables):

Code:
cmd = TESTPATH + ' -s ' + serviceName + ' -r ' + rdir + \
          ' -m ' + masterAcct + ' -p ' + persona + ' -P ' + passwd

# 2  
Old 03-17-2011
Can you give a more concrete command/example?
To use subprocess, maybe you can take a look the codes below, I used
Code:
grep -E -A1 -B1 sys /etc/passwd

as command. it print out the line containing "sys" in my /etc/passwd, also 1 line before and after the matched lines are also printed.

PHP Code:
kentpython
Python 2.6.2 
(release26-maintApr 19 200901:56:41
[
GCC 4.3.3on linux2
Type 
"help""copyright""credits" or "license" for more information.
>>> 
import subprocessshlex
>>> cmd="grep -E -A1 -B1 sys /etc/passwd"
>>> args=shlex.split(cmd)
>>> 
subprocess.Popen(args)
<
subprocess.Popen object at 0xb753220c>
>>> 
bin:x:2:2:bin:/bin:/bin/sh
sys
:x:3:3:sys:/dev:/bin/sh
sync
:x:4:65534:sync:/bin:/bin/sync
--
libuuid:x:100:101::/var/lib/libuuid:/bin/sh
syslog
:x:101:102::/home/syslog:/bin/false
klog
:x:102:103::/home/klog:/bin/false
hplip
:x:103:7:HPLIP system user,,,:/var/run/hplip:/bin/false
avahi
-autoipd:x:104:110:Avahi autoip daemon,,,:/var/lib/avahi-autoipd:/bin/false 
# 3  
Old 03-17-2011
TESTPATH as the name implies is a string containing a path to a binary, it's not a unix command (I wrote it), serviceName,rdir,masterAcct,persona,passwd are variables (strings) like:
rdir = 'hostname.con'
masterAcct = 'guy01'
and so on...

I just managed to get it working like this:
Code:
subprocess.Popen([TESTPAHT, '-s', serviceName, '-r', rdir, '-m', masterAcct, '-p', persona, '-P', passwd])

Please advice, should I use SHELL=true or false?
# 4  
Old 03-17-2011
i think shell should be false, if the command ("binary") is in the 1st element of the list. I have ever used a self-written C program as command, it worked. But if you want to run the command in a particular shell, you should set shell=true, and set executable option.

following texts are taken from python doc.
Quote:
The executable argument specifies the program to execute. It is very seldom needed: Usually, the program to execute is defined by the args argument. If shell=True, the executable argument specifies which shell to use. On Unix, the default shell is /bin/sh. On Windows, the default shell is specified by the COMSPEC environment variable. The only reason you would need to specify shell=True on Windows is where the command you wish to execute is actually built in to the shell, eg dir, copy. You don’t need shell=True to run a batch file, nor to run a console-based executable.
This User Gave Thanks to sk1418 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

[Python] - subprocess issue

So I have this basic script, see below import subprocess import shlex command = "gcloud projects list" subprocess.check_output(shlex.split(command)) subprocess.check_call(shlex.split(command)) The subprocess.check_call(shlex.split(command)) actually return what I expect. It returns... (6 Replies)
Discussion started by: scj2012
6 Replies

2. Programming

Pandas vs Python CSV module

I was wondering is there any occasion where csv module will be desired over pandas. I skipped learning csv module and jumped right into the beautiful pandas and its magical ability to manipulate data. Dataframes are beautiful! (2 Replies)
Discussion started by: srkmish
2 Replies

3. Shell Programming and Scripting

Python-rrdtool try except rrdtool.error module object has no attribute error

I have this code that gives this error on Linux and will be grateful if you can help import rrdtool try: ret_asd = rrdtool.update(myfile.rrd,'N:%s:%s' %(metric1, metric2)); except rrdtool.error, e: print e When i run the above i get the below error except... (1 Reply)
Discussion started by: kaf3773
1 Replies

4. Shell Programming and Scripting

Python subprocess

Hi guys, I'm learning python and perl and i was trying to run from python a perl script using the subprocess module. I have an issue that i don't understand regarding this. I run this code: #!/usr/bin/python import subprocess p2 = subprocess.Popen(,stdout=subprocess.PIPE) output2 =... (2 Replies)
Discussion started by: capitanui
2 Replies

5. Shell Programming and Scripting

Xterm using python subprocess

Hi, I am trying to run a shell script using subprocess in python. I can run simple script with arguments using subprocess.But I am not able to embed xterm in subrocess command. #!/usr/bin/python import subprocess subprocess.call() Above code gives me error. Please help me in... (2 Replies)
Discussion started by: diehard
2 Replies

6. Programming

Web browser module in python

Hi, I am trying to multiple urls in multiple tabs within a browser window. I am using webbrowser module in python: My code: import webbrowser li = for url in li: webbrowser.open_new_tab(url) This code works fine, if the browser is already opened. If the browser is not started... (5 Replies)
Discussion started by: pandeesh
5 Replies

7. Shell Programming and Scripting

suggest a python module for automation..

Im planning to automate my testcases. Please suggest me a python module that will ease my automation scripting. General steps involved in the testcase execution: (all steps below are done in linux machine) 1) start the application under test (AUT) 2) connect to oracle/ms sql db and update... (1 Reply)
Discussion started by: Arun_Linux
1 Replies

8. Shell Programming and Scripting

python: what's wrong with my subprocess.Popen

my script is #!/usr/bin/env python import datetime import subprocess import sys import os import signal from time import sleep def runForAWhile(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) print timeout proc = subprocess.Popen(cmd,... (0 Replies)
Discussion started by: yanglei_fage
0 Replies

9. Shell Programming and Scripting

Looking for a Python CLI module

When I say "Command Line Interface" I mean the wrapper that allows you to type "commands" and have then correspond to pre-programmed actions... all the while making sure that it looks like you are INSIDE a shell of sorts. I had good hope for "common.cli" but that didnt last long, when I found... (1 Reply)
Discussion started by: jjinno
1 Replies
Login or Register to Ask a Question