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?

3
  • What are the args here? In Py3, print is a function, not a statement. Commented Feb 25, 2012 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. Commented Feb 25, 2012 at 11:43
  • Possible duplicate of In Python, how I do use subprocess instead of os.system? Commented Feb 7, 2017 at 14:44

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is there an equivalent for os.startfile() also?
No. os.startfile() is a Windows-specific wrapper around the ShellExecute().
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.