Python Subprocess
Table of Contents

处理子进程, 可以替代 os.system,并提供更丰富的操作。

参考:

  1. https://docs.python.org/2/library/subprocess.html
  2. https://segmentfault.com/a/1190000009176351

基本函数

subprocess.call('ls -l', shell=True)
subprocess.call(['ls', '-l'])
subprocess.Popen('ls -l', shell=True)
subprocess.Popen(['ls', '-l'])
# 读取子进程的stdout
p = subprocess.Popen('ls -l', shell=True, stdout=subprocess.PIPE)
print p.stdout.read()

# 发送数据给子进程的stdin
p = subprocess.Popen('cat', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
stdout, stderr = p.communicate('data1\ndata2\ndata3')
from subprocess import *

output = check_output(["mycmd", "myarg"]) # output=`mycmd myarg`