subprocess模塊是python從2.4版本開始引入的模塊。主要用來取代 一些舊的模塊方法,如os.system、os.spawn*、os.popen*、commands.*等。subprocess經過子進程來執行外部指令,並經過input/output/error管道,獲取子進程的執行的返回信息。python
經常使用方法:shell
subprocess.call():執行命令,並返回執行狀態,其中shell參數爲False時,命令須要經過列表的方式傳入,當shell爲True時,可直接傳入命令python3.x
示例以下:spa
>>> a = subprocess.call(['df','-hT'],shell=False) Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% /boot >>> a = subprocess.call('df -hT',shell=True) Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% /boot
>>> print a
0code
subprocess.check_call():用法與subprocess.call()相似,區別是,當返回值不爲0時,直接拋出異常blog
示例:繼承
>>> a = subprocess.check_call('df -hT',shell=True) Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 94G 64G 26G 72% / tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm /dev/sda1 ext4 976M 56M 853M 7% /boot >>> print a 0 >>> a = subprocess.check_call('dfdsf',shell=True) /bin/sh: dfdsf: command not found Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.6/subprocess.py", line 502, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'dfdsf' returned non-zero exit status 127
subprocess.check_output():用法與上面兩個方法相似,區別是,若是當返回值爲0時,直接返回輸出結果,若是返回值不爲0,直接拋出異常。須要說明的是,該方法在python3.x中才有。進程
subprocess.Popen():字符串
在一些複雜場景中,咱們須要將一個進程的執行輸出做爲另外一個進程的輸入。在另外一些場景中,咱們須要先進入到某個輸入環境,而後再執行一系列的指令等。這個時候咱們就須要使用到suprocess的Popen()方法。該方法有如下參數:input
args:shell命令,能夠是字符串,或者序列類型,如list,tuple。
bufsize:緩衝區大小,可不用關心
stdin,stdout,stderr:分別表示程序的標準輸入,標準輸出及標準錯誤
shell:與上面方法中用法相同
cwd:用於設置子進程的當前目錄
env:用於指定子進程的環境變量。若是env=None,則默認從父進程繼承環境變量
universal_newlines:不一樣系統的的換行符不一樣,當該參數設定爲true時,則表示使用\n做爲換行符
示例1,在/root下建立一個suprocesstest的目錄:
>>> a = subprocess.Popen('mkdir subprocesstest',shell=True,cwd='/root')
示例2,使用python執行幾個命令:
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) obj.stdin.write('print 1 \n') obj.stdin.write('print 2 \n') obj.stdin.write('print 3 \n') obj.stdin.write('print 4 \n') obj.stdin.close() cmd_out = obj.stdout.read() obj.stdout.close() cmd_error = obj.stderr.read() obj.stderr.close() print cmd_out print cmd_error
也能夠使用以下方法:
import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) obj.stdin.write('print 1 \n') obj.stdin.write('print 2 \n') obj.stdin.write('print 3 \n') obj.stdin.write('print 4 \n') out_error_list = obj.communicate() print out_error_list
示例3,將一個子進程的輸出,做爲另外一個子進程的輸入:
import subprocess child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE) child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE) out = child2.communicate()
其餘方法:
import subprocess child = subprocess.Popen('sleep 60',shell=True,stdout=subprocess.PIPE) child.poll() #檢查子進程狀態 child.kill() #終止子進程 child.send_signal() #向子進程發送信號 child.terminate() #終止子進程