1. subprocess.check_output()
2.subprocess.call()
3. subprocess.check_call()
the methods 1.2.3 are are wrapper of subprocess.Popen()
- example 1:
Open Visual Studio through python
This way is incorrect.
>>> retcode = subprocess.call('devenv.exe', shell=True) 'devenv.exe' is not recognized as an internal or external command, operable program or batch file.
Another way:
retcode = subprocess.call("C:Program Files (x86)Microsoft Visual Studio 12.0Common7IDEdevenv.exe",shell=True)
It's done.
Your VS is opened. But the invoke process is blocked. You have to close the process you spawned to get the return code.
- example 2:
try to run iisreset command with python
>>> print subprocess.check_call("iisreset", shell = True) Attempting stop... Internet services successfully stopped Attempting start... Internet services successfully restarted 0
class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
What the Popen returns is a child process you want to create.
example 3:
the Popen() process will not be blocked unless we explicit child.wait()
import subprocess def havewait(): child = subprocess.Popen(['ping','vwijin014']) child.wait() print 'parent process' def nowait(): child = subprocess.Popen(['ping','vwijin014']) #child.wait() print 'parent process'
Pay attention to the red line order. One is before the ping running.