Date Archives September 2016

os.system() vs. subprocess.call()

python_logo

os.system()

  • executed in a subshell, usually bash on linux and OSX, and cmd.exe on windows.
  • The shell will take the string given and interpret the escape characters.
    • ex)  os.system(“python –version”)

subprocess.call()

  • default does not use a shell – it simply tries to run an executable with a name given by string a. You actually can’t pass any arguments in the string a – only the literal name of the executable.
  • To run a command with arguments, I need to give this to subprocess.call as a list
    • ex)  subprocess.call([“python”, “–version”])

 

subprocess.call() can be used same as os.system() by setting shell to true.

  • ex)  subprocess.call(“python –version”, shell=True)