Python executes the shell-subprocess Module
Python executes shell-subprocess module call
Retcode = subprocess. call (["ls", "-l"])
Call I feel like OS. popen. I can only execute the command to check the returned value. Command output is not obtained. So we still need to use Popen. Note that P is capitalized.
Popen basic usage:
Child1 = subprocess. Popen (["cat", "/etc/passwd"], stdout = subprocess. PIPE)
Or:
Child1 = subprocess. Popen ("pwd", stdout = subprocess. PIPE)
Then use communicate () for interaction. Value
Res = child1.communicate () type (res)
The res return value is a tuple type value (an unchangeable array ). To obtain the command output, you only need to take the first parameter [0] After it:
Res2 = child1.communicate () [0]
Communicate () returns a tuple (stdout, stderr ).
If multiple commands and pipelines are connected, it is like this:
Child1 = subprocess. popen (["cat", "/etc/passwd"], stdout = subprocess. PIPE) child2 = subprocess. popen (["grep", "0: 0"], stdin = child1.stdout, stdout = subprocess. PIPE) out = child2.communicate () [0] print out