1

I have a string called variable and need to do the subprocess equivalent of os.system. I've tried to find a way to do this but have only found:

variable2 = subprocess.Popen(args, stdout=subprocess.PIPE)
print variable2.communicate()[0]

However, I'm having trouble understanding how to use it. How do I achieve my goal?

Eden Crow
  • 14,684
  • 11
  • 26
  • 24
  • What are the `args` here? In Py3, `print` is a function, not a statement. – Zaur Nasibov Feb 25 '12 at 11:36
  • The code block is copy/pasted because I can't find a way of doing what I need myself, hence the question. – Eden Crow Feb 25 '12 at 11:43
  • Possible duplicate of [In Python, how I do use subprocess instead of os.system?](http://stackoverflow.com/questions/421206/in-python-how-i-do-use-subprocess-instead-of-os-system) – Tom Myddeltyn Feb 07 '17 at 14:44

3 Answers3

3

The documentation provides equivalents for several old-style sub-process creation functions. os.system() is explained here.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
1
In [4]: os.system('uname -a')
Linux diego-workstation 3.0.0-16-generic #28-Ubuntu SMP Fri Jan 27 17:44:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Out[4]: 0

In [8]: subprocess.call(['uname', '-a'])
Linux diego-workstation 3.0.0-16-generic #28-Ubuntu SMP Fri Jan 27 17:44:39 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
Out[8]: 0
Diego Navarro
  • 9,316
  • 3
  • 26
  • 33
0

Look at the subprocess.call, subprocess.check_call and subprocess.check_output functions. You may need to pass shell=True if you are executing a shell command (as would be given to os.system) rather than explicitly specifying the executable and a sequence of arguments.

gps
  • 1,360
  • 12
  • 12