5

I have two python scripts. The first script calls a table of second scripts in which I need to execute a third party python script. It looks something like this:

# the call from the first script. 
cmd = "qsub -sync y -b -cwd -V -q long  -t 1-10 -tc 5 -N 'script_two' ./script2.py"

script2thread = pexpect.spawn(cmd)

# end of script 1 

So here i am sending 10 jobs out to the queue. In script 2 I have a case statement based on the task_id. In each one I make a similar call to the third party script using different parameters.

...
elif(task_id == 4)
subprocess.call(./script3)

# or 

os.system(./script3 , shell=True)

This is where my question lies. Is there a difference/benefit to using one or the other? I know that on windows using one over the other makes a big difference because of support issues but I am on linux and have no intention of running this on windows. Sometimes I get very weird results from using the subprocess, it cannot find other things on the network that it can when the third script is run independently one at a time.

alex_milhouse
  • 853
  • 1
  • 14
  • 30
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Nabin Nov 22 '17 at 14:00

1 Answers1

15

You should use subprocess. Not that it makes any difference, it's just a newer module intended to replace os.system (have a look at this section for a drop-in replacement). It also has more features in case you need them one day.

In short: there is no reason to use os.system (except for compatibility with older versions of Python).

kirelagin
  • 12,085
  • 1
  • 35
  • 54
  • 2
    In case you don't need the new features: Is there a reason to use subprocess? (An advantage of `os.system` is the easier syntax) – Martin Thoma Aug 10 '17 at 08:48
  • @MartinThoma No, I don’t think there is one. Well, the documentation mentions that the return value of `os.system` is OS-specific, so error handling might become less reliable. As a side note, I don’t see how `os.system("mycmd" + " myarg")` is easier than `subprocess.call("mycmd" + " myarg", shell=True)`. – kirelagin Aug 11 '17 at 11:02
  • Isn't it necessary for subprocess to split all arguments to a list while you can give a single string to `os.system` no matter how long your command is? – Martin Thoma Aug 11 '17 at 11:05
  • @MartinThoma Ah, yes, you are right! But not if you also pass `shell=True`. – kirelagin Aug 11 '17 at 11:07
  • so what @MartinThoma hinted to - avoiding to pass a list - translates into: `subprocess.Popen("mycmd" + " myarg", shell=True)` – Agile Bean Jul 28 '18 at 07:59