Skip to content
Advertisement

How to run multiple linux commands as a sequence of arguments through subprocess.Popen Python

code-1 : Passing linux commands as a sequence of arguments

from subprocess import Popen, PIPE

run_cmd = Popen(['ls','-l','mkdir','hello'], stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode

Output – 1:

ls: cannot access mkdir: No such file or directory
ls: cannot access hello: No such file or directory
 2

In the above code I am trying to run multiple linux commands by passing them as a sequence of arguments. If I am modifying the above code to the following one it works fine.

code-2 : Pass linux commands as a string

from subprocess import Popen, PIPE

run_cmd = Popen('mkdir hello; ls -l; echo Hello; rm -r hello', shell=True, stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode

Output – 2 :

drwxrwxr-x. 2 sujatap sujatap    6 May  9 21:28 hello
-rw-rw-r--. 1 sujatap sujatap   53 May  8 20:51 test.py
Hello
0

As shell=True is not a suggested way to be used so I want to run the linux commands using the former one. Thanks.

Advertisement

Answer

If something does not work, check its documentation: https://docs.python.org/2/library/subprocess.html#popen-constructor

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

So test your single program runs first (list of program and its arguments), then make a list of lists and run them in sequence with a loop:

myprogramsequence = [
    ["ls", "-l"],
    ["mkdir", "hello"]
]

for argumentlist in myprogramsequence:
    run_cmd = Popen( argumentlist, ...
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement