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.