Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: subprocess call with shell=False not working

I am using Python script to invoke a Java virtual machine. The following command works:

subprocess.call(["./rvm"], shell=False)  # works
subprocess.call(["./rvm xyz"], shell=True) # works

But,

subprocess.call(["./rvm xyz"], shell=False) # not working

does not work. Python documentation advices to avoid shell=True.

like image 695
dev Avatar asked Aug 23 '14 19:08

dev


1 Answers

You need to split the commands into separate strings:

subprocess.call(["./rvm", "xyz"], shell=False)

A string will work when shell=True but you need a list of args when shell=False

The shlex module is useful more so for more complicated commands and dealing with input but good to learn about:

import shlex

cmd = "python  foo.py"
subprocess.call(shlex.split(cmd), shell=False)

shlex tut

like image 55
Padraic Cunningham Avatar answered Sep 29 '22 12:09

Padraic Cunningham