欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python模塊學(xué)習(xí)之subprocess詳解

 更新時(shí)間:2023年08月27日 16:25:34   作者:追憶MHyourh  
subprocess是Python?2.4中新增的一個(gè)模塊,它允許你生成新的進(jìn)程,連接到它們的?input/output/error?管道,并獲取它們的返回(狀態(tài))碼,下面小編就來(lái)和大家聊聊它的具體使用吧

一.subprocess模塊

subprocess是Python 2.4中新增的一個(gè)模塊,它允許你生成新的進(jìn)程,連接到它們的 input/output/error 管道,并獲取它們的返回(狀態(tài))碼。這個(gè)模塊的目的在于替換幾個(gè)舊的模塊和方法,如:

os.system
os.spawn*

1.subprocess模塊中的常用函數(shù)

函數(shù)描述
subprocess.run()Python 3.5中新增的函數(shù)。執(zhí)行指定的命令,等待命令執(zhí)行完成后返回一個(gè)包含執(zhí)行結(jié)果的CompletedProcess類的實(shí)例。
subprocess.call()執(zhí)行指定的命令,返回命令執(zhí)行狀態(tài),其功能類似于os.system(cmd)。
subprocess.check_call()Python 2.5中新增的函數(shù)。 執(zhí)行指定的命令,如果執(zhí)行成功則返回狀態(tài)碼,否則拋出異常。其功能等價(jià)于subprocess.run(…, check=True)。
subprocess.check_output()Python 2.7中新增的的函數(shù)。執(zhí)行指定的命令,如果執(zhí)行狀態(tài)碼為0則返回命令執(zhí)行結(jié)果,否則拋出異常。
subprocess.getoutput(cmd)接收字符串格式的命令,執(zhí)行命令并返回執(zhí)行結(jié)果,其功能類似于os.popen(cmd).read()和commands.getoutput(cmd)。
subprocess.getstatusoutput(cmd)執(zhí)行cmd命令,返回一個(gè)元組(命令執(zhí)行狀態(tài), 命令執(zhí)行結(jié)果輸出),其功能類似于commands.getstatusoutput()。

說(shuō)明:

1.在Python 3.5之后的版本中,官方文檔中提倡通過(guò)subprocess.run()函數(shù)替代其他函數(shù)來(lái)使用subproccess模塊的功能;

2.在Python 3.5之前的版本中,我們可以通過(guò)subprocess.call(),subprocess.getoutput()等上面列出的其他函數(shù)來(lái)使用subprocess模塊的功能;

3.subprocess.run()、subprocess.call()、subprocess.check_call()和subprocess.check_output()都是通過(guò)對(duì)subprocess.Popen的封裝來(lái)實(shí)現(xiàn)的高級(jí)函數(shù),因此如果我們需要更復(fù)雜功能時(shí),可以通過(guò)subprocess.Popen來(lái)完成。

4.subprocess.getoutput()和subprocess.getstatusoutput()函數(shù)是來(lái)自Python 2.x的commands模塊的兩個(gè)遺留函數(shù)。它們隱式的調(diào)用系統(tǒng)shell,并且不保證其他函數(shù)所具有的安全性和異常處理的一致性。另外,它們從Python 3.3.4開(kāi)始才支持Windows平臺(tái)。

2.上面各函數(shù)的定義及參數(shù)說(shuō)明

函數(shù)參數(shù)列表:

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False, universal_newlines=False)
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False, timeout=None)
subprocess.getstatusoutput(cmd)
subprocess.getoutput(cmd)

參數(shù)說(shuō)明:

args: 要執(zhí)行的shell命令,默認(rèn)應(yīng)該是一個(gè)字符串序列,如[‘df’, ‘-Th’]或(‘df’, ‘-Th’),也可以是一個(gè)字符串,如’df -Th’,但是此時(shí)需要把shell參數(shù)的值置為True。

shell: 如果shell為True,那么指定的命令將通過(guò)shell執(zhí)行。如果我們需要訪問(wèn)某些shell的特性,如管道、文件名通配符、環(huán)境變量擴(kuò)展功能,這將是非常有用的。當(dāng)然,python本身也提供了許多類似shell的特性的實(shí)現(xiàn),如glob、fnmatch、os.walk()、os.path.expandvars()、os.expanduser()和shutil等。

check: 如果check參數(shù)的值是True,且執(zhí)行命令的進(jìn)程以非0狀態(tài)碼退出,則會(huì)拋出一個(gè)CalledProcessError的異常,且該異常對(duì)象會(huì)包含 參數(shù)、退出狀態(tài)碼、以及stdout和stderr(如果它們有被捕獲的話)。

stdout, stderr:input: 該參數(shù)是傳遞給Popen.communicate(),通常該參數(shù)的值必須是一個(gè)字節(jié)序列,如果universal_newlines=True,則其值應(yīng)該是一個(gè)字符串。

  • run()函數(shù)默認(rèn)不會(huì)捕獲命令執(zhí)行結(jié)果的正常輸出和錯(cuò)誤輸出,如果我們向獲取這些內(nèi)容需要傳遞subprocess.PIPE,然后可以通過(guò)返回的CompletedProcess類實(shí)例的stdout和stderr屬性或捕獲相應(yīng)的內(nèi)容;
  • call()和check_call()函數(shù)返回的是命令執(zhí)行的狀態(tài)碼,而不是CompletedProcess類實(shí)例,所以對(duì)于它們而言,stdout和stderr不適合賦值為subprocess.PIPE;
  • check_output()函數(shù)默認(rèn)就會(huì)返回命令執(zhí)行結(jié)果,所以不用設(shè)置stdout的值,如果我們希望在結(jié)果中捕獲錯(cuò)誤信息,可以執(zhí)行stderr=subprocess.STDOUT。

universal_newlines: 該參數(shù)影響的是輸入與輸出的數(shù)據(jù)格式,比如它的值默認(rèn)為False,此時(shí)stdout和stderr的輸出是字節(jié)序列;當(dāng)該參數(shù)的值設(shè)置為True時(shí),stdout和stderr的輸出是字符串。

3.subprocess.CompletedProcess類介紹

需要說(shuō)明的是,subprocess.run()函數(shù)是Python3.5中新增的一個(gè)高級(jí)函數(shù),其返回值是一個(gè)subprocess.CompletedPorcess類的實(shí)例,因此,subprocess.completedPorcess類也是Python 3.5中才存在的。它表示的是一個(gè)已結(jié)束進(jìn)程的狀態(tài)信息,它所包含的屬性如下:

  • args: 用于加載該進(jìn)程的參數(shù),這可能是一個(gè)列表或一個(gè)字符串
  • returncode: 子進(jìn)程的退出狀態(tài)碼。通常情況下,退出狀態(tài)碼為0則表示進(jìn)程成功運(yùn)行了;一個(gè)負(fù)值-N表示這個(gè)子進(jìn)程被信號(hào)N終止了
  • stdout: 從子進(jìn)程捕獲的stdout。這通常是一個(gè)字節(jié)序列,如果run()函數(shù)被調(diào)用時(shí)指定universal_newlines=True,則該屬性值是一個(gè)字符串。如果run()函數(shù)被調(diào)用時(shí)指定stderr=subprocess.STDOUT,那么stdout和stderr將會(huì)被整合到這一個(gè)屬性中,且stderr將會(huì)為None
  • stderr: 從子進(jìn)程捕獲的stderr。它的值與stdout一樣,是一個(gè)字節(jié)序列或一個(gè)字符串。如果stderr滅有被捕獲的話,它的值就為None
  • check_returncode(): 如果returncode是一個(gè)非0值,則該方法會(huì)拋出一個(gè)CalledProcessError異常。

4.實(shí)例

subprocess.run()

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

subprocess.call()

>>> subprocess.call(['ls',  '-l'])
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
0
>>> subprocess.call('ls -l', shell=True)
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
0
>>> subprocess.call(['ls',  '-l'], stdout=subprocess.DEVNULL)
0
>>> subprocess.call(['ls',  '-l', '/test'])
ls: 無(wú)法訪問(wèn)/test: 沒(méi)有那個(gè)文件或目錄
2

suprocess.check_call()

>>> subprocess.check_call(['ls',  '-l'])
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
0
>>> subprocess.check_call('ls -l', shell=True)
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
0
>>> subprocess.check_call('ls -l /test', shell=True)
ls: 無(wú)法訪問(wèn)/test: 沒(méi)有那個(gè)文件或目錄
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/subprocess.py", line 557, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'ls -l /test' returned non-zero exit status 2

subprocess.check_output()

>>> ret = subprocess.check_output(['ls',  '-l'])
>>> print(ret)
b' \xe5\x85\xac\xe5\x85\xb1\xe7\x9a\x84\ndrwxr-xr-x  2 wader wader   4096 12\xe6\x9c\x88  7  2015 \xe6\xa8\xa1\xe6\x9d\xbf\ndrwxr-xr-x  2 wader wader   4096 12\xe6\x9c\x88  7  2015 \xe8\xa7\x86\xe9\xa2\x91\ndrwxr-xr-x  2 wader wader   4096 12\xe6\x9c\x88  7  2015 \xe5\x9b\xbe\xe7\x89\x87\ndrwxr-xr-x  2 wader wader   4096 12\xe6\x9c\x88  7  2015 \xe6\x96\x87\xe6\xa1\xa3\ndrwxr-xr-x  2 wader wader   4096  4\xe6\x9c\x88 13  2016 \xe4\xb8\x8b\xe8\xbd\xbd\ndrwxr-xr-x  2 wader wader   4096 12\xe6\x9c\x88  7  2015 \xe9\x9f\xb3\xe4\xb9\x90\ndrwxr-xr-x  7 wader wader   4096  5\xe6\x9c\x88 26  2016 \xe6\xa1\x8c\xe9\x9d\xa2\n'
>>> ret = subprocess.check_output(['ls',  '-l'], universal_newlines=True)
>>> print(ret)
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面

subprocess.getoutput()與subprocess.getstatusoutput()

>>> ret = subprocess.getoutput('ls -l')
>>> print(ret)
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
>>> retcode, output = subprocess.getstatusoutput('ls -l')
>>> print(retcode)
0
>>> print(output)
總用量 160
drwxr-xr-x  2 wader wader   4096 12月  7  2015 公共的
drwxr-xr-x  2 wader wader   4096 12月  7  2015 模板
drwxr-xr-x  2 wader wader   4096 12月  7  2015 視頻
drwxr-xr-x  2 wader wader   4096 12月  7  2015 圖片
drwxr-xr-x  2 wader wader   4096 12月  7  2015 文檔
drwxr-xr-x  2 wader wader   4096  4月 13  2016 下載
drwxr-xr-x  2 wader wader   4096 12月  7  2015 音樂(lè)
drwxr-xr-x  7 wader wader   4096  5月 26  2016 桌面
>>> retcode, output = subprocess.getstatusoutput('ls -l /test')
>>> print(retcode)
2
>>> print(output)
ls: 無(wú)法訪問(wèn)/test: 沒(méi)有那個(gè)文件或目錄

二.subprocess.Popen介紹

該類用于在一個(gè)新的進(jìn)程中執(zhí)行一個(gè)子程序。前面我們提到過(guò),上面介紹的這些函數(shù)都是基于subprocess.Popen類實(shí)現(xiàn)的,通過(guò)使用這些被封裝后的高級(jí)函數(shù)可以很方面的完成一些常見(jiàn)的需求。由于subprocess模塊底層的進(jìn)程創(chuàng)建和管理是由Popen類來(lái)處理的,因此,當(dāng)我們無(wú)法通過(guò)上面哪些高級(jí)函數(shù)來(lái)實(shí)現(xiàn)一些不太常見(jiàn)的功能時(shí)就可以通過(guò)subprocess.Popen類提供的靈活的api來(lái)完成。

1 subprocess.Popen的構(gòu)造函數(shù)

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, 
    preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False,
    startup_info=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=())

參數(shù)說(shuō)明:

  • args: 要執(zhí)行的shell命令,可以是字符串,也可以是命令各個(gè)參數(shù)組成的序列。當(dāng)該參數(shù)的值是一個(gè)字符串時(shí),該命令的解釋過(guò)程是與平臺(tái)相關(guān)的,因此通常建議將args參數(shù)作為一個(gè)序列傳遞。
  • bufsize: 指定緩存策略,0表示不緩沖,1表示行緩沖,其他大于1的數(shù)字表示緩沖區(qū)大小,負(fù)數(shù) 表示使用系統(tǒng)默認(rèn)緩沖策略。stdin, stdout, stderr: 分別表示程序標(biāo)準(zhǔn)輸入、輸出、錯(cuò)誤句柄。
  • preexec_fn: 用于指定一個(gè)將在子進(jìn)程運(yùn)行之前被調(diào)用的可執(zhí)行對(duì)象,只在Unix平臺(tái)下有效。
  • close_fds: 如果該參數(shù)的值為True,則除了0,1和2之外的所有文件描述符都將會(huì)在子進(jìn)程執(zhí)行之前被關(guān)閉。
  • shell: 該參數(shù)用于標(biāo)識(shí)是否使用shell作為要執(zhí)行的程序,如果shell值為True,則建議將args參數(shù)作為一個(gè)字符串傳遞而不要作為一個(gè)序列傳遞。
  • cwd: 如果該參數(shù)值不是None,則該函數(shù)將會(huì)在執(zhí)行這個(gè)子進(jìn)程之前改變當(dāng)前工作目錄。
  • env: 用于指定子進(jìn)程的環(huán)境變量,如果env=None,那么子進(jìn)程的環(huán)境變量將從父進(jìn)程中繼承。如果env!=None,它的值必須是一個(gè)映射對(duì)象。
  • universal_newlines: 如果該參數(shù)值為True,則該文件對(duì)象的stdin,stdout和stderr將會(huì)作為文本流被打開(kāi),否則他們將會(huì)被作為二進(jìn)制流被打開(kāi)。
  • startupinfo和creationflags: 這兩個(gè)參數(shù)只在Windows下有效,它們將被傳遞給底層的CreateProcess()函數(shù),用于設(shè)置子進(jìn)程的一些屬性,如主窗口的外觀,進(jìn)程優(yōu)先級(jí)等。

2.subprocess.Popen類的實(shí)例可調(diào)用的方法

方法描述
Popen.poll()用于檢查子進(jìn)程(命令)是否已經(jīng)執(zhí)行結(jié)束,沒(méi)結(jié)束返回None,結(jié)束后返回狀態(tài)碼。
Popen.wait(timeout=None)等待子進(jìn)程結(jié)束,并返回狀態(tài)碼;如果在timeout指定的秒數(shù)之后進(jìn)程還沒(méi)有結(jié)束,將會(huì)拋出一個(gè)TimeoutExpired異常。
Popen.communicate(input=None, timeout=None)該方法可用來(lái)與進(jìn)程進(jìn)行交互,比如發(fā)送數(shù)據(jù)到stdin,從stdout和stderr讀取數(shù)據(jù),直到到達(dá)文件末尾。
Popen.send_signal(signal)發(fā)送指定的信號(hào)給這個(gè)子進(jìn)程。
Popen.terminate()停止該子進(jìn)程。
Popen.kill()殺死該子進(jìn)程。

關(guān)于communicate()方法的說(shuō)明:

  • 該方法中的可選參數(shù) input 應(yīng)該是將被發(fā)送給子進(jìn)程的數(shù)據(jù),或者如沒(méi)有數(shù)據(jù)發(fā)送給子進(jìn)程,該參數(shù)應(yīng)該是None。input參數(shù)的數(shù)據(jù)類型必須是字節(jié)串,如果universal_newlines參數(shù)值為True,則input參數(shù)的數(shù)據(jù)類型必須是字符串。
  • 該方法返回一個(gè)元組(stdout_data, stderr_data),這些數(shù)據(jù)將會(huì)是字節(jié)穿或字符串(如果universal_newlines的值為True)。
  • 如果在timeout指定的秒數(shù)后該進(jìn)程還沒(méi)有結(jié)束,將會(huì)拋出一個(gè)TimeoutExpired異常。捕獲這個(gè)異常,然后重新嘗試通信不會(huì)丟失任何輸出的數(shù)據(jù)。但是超時(shí)之后子進(jìn)程并沒(méi)有被殺死,為了合理的清除相應(yīng)的內(nèi)容,一個(gè)好的應(yīng)用應(yīng)該手動(dòng)殺死這個(gè)子進(jìn)程來(lái)結(jié)束通信。
  • 需要注意的是,這里讀取的數(shù)據(jù)是緩沖在內(nèi)存中的,所以,如果數(shù)據(jù)大小非常大或者是無(wú)限的,就不應(yīng)該使用這個(gè)方法

3 subprocess.Popen使用實(shí)例

實(shí)例1

>>> import subprocess
>>>
>>> p = subprocess.Popen('df -Th', stdout=subprocess.PIPE, shell=True)
>>> print(p.stdout.read())
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G   12G   26G  31% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G  386M  3.5G  10% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/0
tmpfs          tmpfs     783M     0  783M   0% /run/user/1000

實(shí)例2

>>> 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')
>>> out,err = obj.communicate()
>>> print(out)
1
2
3
>>> print(err)

實(shí)例3

>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> out,err = obj.communicate(input='print(1) \n')
>>> print(out)
1
>>> print(err)

實(shí)例4

實(shí)現(xiàn)類似df -Th | grep data命令的功能,實(shí)際上就是實(shí)現(xiàn)shell中管道的共功能。

>>> 
>>> p1 = subprocess.Popen(['df', '-Th'], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(['grep', 'data'], stdin=p1.stdout, stdout=subprocess.PIPE)
>>> out,err = p2.communicate()
>>> print(out)
/dev/vdb1      ext4      493G  4.8G  463G   2% /data
/dev/vdd1      ext4     1008G  420G  537G  44% /data1
/dev/vde1      ext4      985G  503G  432G  54% /data2
#Python小白學(xué)習(xí)交流群:711312441
>>> print(err)
None

三.總結(jié)

那么我們到底該用哪個(gè)模塊、哪個(gè)函數(shù)來(lái)執(zhí)行命令與系統(tǒng)及系統(tǒng)進(jìn)行交互呢?下面我們來(lái)做個(gè)總結(jié):

首先應(yīng)該知道的是,Python2.4版本引入了subprocess模塊用來(lái)替換os.system()、os.popen()、os.spawn*()等函數(shù)以及commands模塊;也就是說(shuō)如果你使用的是Python 2.4及以上的版本就應(yīng)該使用subprocess模塊了。

如果你的應(yīng)用使用的Python 2.4以上,但是是Python 3.5以下的版本,Python官方給出的建議是使用subprocess.call()函數(shù)。Python 2.5中新增了一個(gè)subprocess.check_call()函數(shù),Python 2.7中新增了一個(gè)subprocess.check_output()函數(shù),這兩個(gè)函數(shù)也可以按照需求進(jìn)行使用。

如果你的應(yīng)用使用的是Python 3.5及以上的版本(目前應(yīng)該還很少),Python官方給出的建議是盡量使用subprocess.run()函數(shù)。

當(dāng)subprocess.call()、subprocess.check_call()、subprocess.check_output()和subprocess.run()這些高級(jí)函數(shù)無(wú)法滿足需求時(shí),我們可以使用subprocess.Popen類來(lái)實(shí)現(xiàn)我們需要的復(fù)雜功能。

到此這篇關(guān)于Python模塊學(xué)習(xí)之subprocess詳解的文章就介紹到這了,更多相關(guān)Python subprocess模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel())

    matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel())

    這篇文章主要介紹了matplotlib之pyplot模塊坐標(biāo)軸標(biāo)簽設(shè)置使用(xlabel()、ylabel()),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python同時(shí)處理多個(gè)異常的方法

    Python同時(shí)處理多個(gè)異常的方法

    這篇文章主要介紹了Python同時(shí)處理多個(gè)異常的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • Python中的pprint模塊

    Python中的pprint模塊

    本文介紹了 Python中的pprint模塊,pprint模塊包含一個(gè)“美觀打印機(jī)”,用于生成數(shù)據(jù)結(jié)構(gòu)的一個(gè)美觀的視圖。格式化工具會(huì)生成數(shù)據(jù)結(jié)構(gòu)的一些表示,不僅能夠由解釋器正確地解析,還便于人閱讀。輸出會(huì)盡可能放在一行上,分解為多行時(shí)會(huì)縮進(jìn),想了解具體內(nèi)容請(qǐng)參考下文
    2021-11-11
  • python制作簡(jiǎn)單五子棋游戲

    python制作簡(jiǎn)單五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了python制作簡(jiǎn)單五子棋游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • python 使用while循環(huán)輸出*組成的菱形實(shí)例

    python 使用while循環(huán)輸出*組成的菱形實(shí)例

    這篇文章主要介紹了python 使用while循環(huán)輸出*組成的菱形實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python數(shù)據(jù)報(bào)表之Excel操作模塊用法分析

    Python數(shù)據(jù)報(bào)表之Excel操作模塊用法分析

    這篇文章主要介紹了Python數(shù)據(jù)報(bào)表之Excel操作模塊用法,結(jié)合實(shí)例形式分析了XlsxWriter模塊的功能及簡(jiǎn)單使用方法,需要的朋友可以參考下
    2019-03-03
  • Python 實(shí)現(xiàn)國(guó)產(chǎn)SM3加密算法的示例代碼

    Python 實(shí)現(xiàn)國(guó)產(chǎn)SM3加密算法的示例代碼

    這篇文章主要介紹了Python 實(shí)現(xiàn)國(guó)產(chǎn)SM3加密算法的示例代碼,幫助大家更好的理解和學(xué)習(xí)密碼學(xué),感興趣的朋友可以了解下
    2020-09-09
  • python第三方庫(kù)學(xué)習(xí)筆記

    python第三方庫(kù)學(xué)習(xí)筆記

    在本篇文章里小編給大家整理了關(guān)于python第三方庫(kù)學(xué)習(xí)筆記相關(guān)內(nèi)容,有需要的朋友們可以參考下。
    2020-02-02
  • Python調(diào)用SQLPlus來(lái)操作和解析Oracle數(shù)據(jù)庫(kù)的方法

    Python調(diào)用SQLPlus來(lái)操作和解析Oracle數(shù)據(jù)庫(kù)的方法

    這篇文章主要介紹了Python調(diào)用SQLPlus來(lái)操作和解析Oracle數(shù)據(jù)庫(kù)的方法,這樣用SQL*Plus方式來(lái)分析Oracle中的數(shù)據(jù)就變得十分方便,需要的朋友可以參考下
    2016-04-04
  • Python第三方Window模塊文件的幾種安裝方法

    Python第三方Window模塊文件的幾種安裝方法

    這篇文章主要介紹了Python第三方Window模塊文件安裝方法,需要的朋友可以參考下
    2018-11-11

最新評(píng)論