最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python模塊之subprocess模塊級方法的使用

 更新時間:2019年03月26日 14:34:26   作者:當(dāng)麻的小紅箱  
這篇文章主要介紹了python模塊之subprocess模塊級方法的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

subprocess.run()

運行并等待args參數(shù)指定的指令完成,返回CompletedProcess實例。

參數(shù):(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs)。除input, capture_output, timeout, check,其他參數(shù)與Popen構(gòu)造器參數(shù)一致。

capture_output:如果設(shè)置為True,表示重定向stdout和stderr到管道,且不能再傳遞stderr或stdout參數(shù),否則拋出異常。

input:input參數(shù)將作為子進程的標(biāo)準(zhǔn)輸入傳遞給Popen.communicate()方法,必須是string(需要指定encoding或errors參數(shù),或者設(shè)置text為True)或byte類型。非None的input參數(shù)不能和stdin參數(shù)一起使用,否則將拋出異常,構(gòu)造Popen實例的stdin參數(shù)將指定為subprocess.PIPE。

timeout:傳遞給Popen.communicate()方法。

check:如果設(shè)置為True,進程執(zhí)行返回非0狀態(tài)碼將拋出CalledProcessError異常。

# 源碼

def run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs):
  if input is not None:
    if 'stdin' in kwargs:
      raise ValueError('stdin and input arguments may not both be used.')
    kwargs['stdin'] = PIPE
  
  if capture_output:
    if ('stdout' in kwargs) or ('stderr' in kwargs):
      raise ValueError('stdout and stderr arguments may not be used '
               'with capture_output.')
    kwargs['stdout'] = PIPE
    kwargs['stderr'] = PIPE
  
  with Popen(*popenargs, **kwargs) as process:
    try:
      stdout, stderr = process.communicate(input, timeout=timeout)
    except TimeoutExpired:
      process.kill()
      stdout, stderr = process.communicate()
      raise TimeoutExpired(process.args, timeout, output=stdout,
                 stderr=stderr)
    except: # Including KeyboardInterrupt, communicate handled that.
      process.kill()
      # We don't call process.wait() as .__exit__ does that for us.
      raise
    retcode = process.poll()
    if check and retcode:
      raise CalledProcessError(retcode, process.args,
                   output=stdout, stderr=stderr)
  return CompletedProcess(process.args, retcode, stdout, stderr)

python3.5版本前,call(), check_all(), checkoutput()三種方法構(gòu)成了subprocess模塊的高級API。

subprocess.call()

運行并等待args參數(shù)指定的指令完成,返回執(zhí)行狀態(tài)碼(Popen實例的returncode屬性)。

參數(shù):(*popenargs, timeout=None, **kwargs)。與Popen構(gòu)造器參數(shù)基本相同,除timeout外的所有參數(shù)都將傳遞給Popen接口。

調(diào)用call()函數(shù)不要使用stdout=PIPE或stderr=PIPE,因為如果子進程生成了足量的輸出到管道填滿OS管道緩沖區(qū),子進程將因不能從管道讀取數(shù)據(jù)而導(dǎo)致阻塞。

# 源碼
def call(*popenargs, timeout=None, **kwargs):
  with Popen(*popenargs, **kwargs) as p:
    try:
      return p.wait(timeout=timeout)
    except:
      p.kill()
      p.wait()
      raise

subprocess.check_call()

運行并等待args參數(shù)指定的指令完成,返回0狀態(tài)碼或拋出CalledProcessError異常,該異常的cmd和returncode屬性可以查看執(zhí)行異常的指令和狀態(tài)碼。

參數(shù):(*popenargs, **kwargs)。全部參數(shù)傳遞給call()函數(shù)。

注意事項同call()

# 源碼
def check_call(*popenargs, **kwargs):
  retcode = call(*popenargs, **kwargs)
  if retcode:
    cmd = kwargs.get("args")
    if cmd is None:
      cmd = popenargs[0]
    raise CalledProcessError(retcode, cmd)
  return 0

subprocess.check_output()

運行并等待args參數(shù)指定的指令完成,返回標(biāo)準(zhǔn)輸出(CompletedProcess實例的stdout屬性),類型默認(rèn)是byte字節(jié),字節(jié)編碼可能取決于執(zhí)行的指令,設(shè)置universal_newlines=True可以返回string類型的值。
如果執(zhí)行狀態(tài)碼非0,將拋出CalledProcessError異常。

參數(shù):(*popenargs, timeout=None, **kwargs)。全部參數(shù)傳遞給run()函數(shù),但不支持顯示地傳遞input=None繼承父進程的標(biāo)準(zhǔn)輸入文件句柄。

要在返回值中捕獲標(biāo)準(zhǔn)錯誤,設(shè)置stderr=subprocess.STDOUT;也可以將標(biāo)準(zhǔn)錯誤重定向到管道stderr=subprocess.PIPE,通過CalledProcessError異常的stderr屬性訪問。

# 源碼

def check_output(*popenargs, timeout=None, **kwargs):
  if 'stdout' in kwargs:
    raise ValueError('stdout argument not allowed, it will be overridden.')

  if 'input' in kwargs and kwargs['input'] is None:
    # Explicitly passing input=None was previously equivalent to passing an
    # empty string. That is maintained here for backwards compatibility.
    kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''

  return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
        **kwargs).stdout

subprocess模塊還提供了python2.x版本中commands模塊的相關(guān)函數(shù)。

subprocess.getstatusoutput(cmd)

實際上是調(diào)用check_output()函數(shù),在shell中執(zhí)行string類型的cmd指令,返回(exitcode, output)形式的元組,output(包含stderrstdout)是使用locale encoding解碼的字符串,并刪除了結(jié)尾的換行符。

# 源碼
try:
  data = check_output(cmd, shell=True, universal_newlines=True, stderr=STDOUT)
  exitcode = 0
except CalledProcessError as ex:
  data = ex.output
  exitcode = ex.returncode
if data[-1:] == '\n':
  data = data[:-1]
return exitcode, data

subprocess.getoutput(cmd)

getstatusoutput()類似,但結(jié)果只返回output。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python爬蟲的一個常見簡單js反爬詳解

    python爬蟲的一個常見簡單js反爬詳解

    這篇文章主要介紹了python爬蟲的一個常見簡單js反爬詳解我們在寫爬蟲是遇到最多的應(yīng)該就是js反爬了,今天分享一個比較常見的js反爬,我把js反爬分為參數(shù)由js加密生成和js生成cookie等來操作瀏覽器這兩部分,需要的朋友可以參考下
    2019-07-07
  • python3模擬百度登錄并實現(xiàn)百度貼吧簽到示例分享(百度貼吧自動簽到)

    python3模擬百度登錄并實現(xiàn)百度貼吧簽到示例分享(百度貼吧自動簽到)

    這篇文章主要介紹了python3模擬百度登錄并實現(xiàn)百度貼吧簽到示例,需要的朋友可以參考下
    2014-02-02
  • Python中多繼承與菱形繼承問題的解決方案與實踐

    Python中多繼承與菱形繼承問題的解決方案與實踐

    在Python這個靈活且功能強大的編程語言中,多繼承是一個既強大又復(fù)雜的概念,它允許一個類繼承自多個父類,從而能夠復(fù)用多個父類的屬性和方法,本文將深入解釋Python中的多繼承概念,詳細剖析菱形繼承問題,并探討Python是如何解決這一難題的,需要的朋友可以參考下
    2024-07-07
  • python如何制作縮略圖

    python如何制作縮略圖

    python如何制作縮略圖?這篇文章主要為大家詳細介紹了python制作縮略圖的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 詳解python中讀取和查看圖片的6種方法

    詳解python中讀取和查看圖片的6種方法

    本文主要介紹了詳解python中讀取和查看圖片的6種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • python通過socket搭建極簡web服務(wù)器的實現(xiàn)代碼

    python通過socket搭建極簡web服務(wù)器的實現(xiàn)代碼

    python的web框架眾多,常見的如django、flask、tornado等,其底層是什么還是有些許的疑問,所以查找相關(guān)資料,實現(xiàn)瀏覽器訪問,并返回相關(guān)信息,本文將給大家介紹python通過socket搭建極簡web服務(wù)器,需要的朋友可以參考下
    2023-10-10
  • python遞歸&迭代方法實現(xiàn)鏈表反轉(zhuǎn)

    python遞歸&迭代方法實現(xiàn)鏈表反轉(zhuǎn)

    這篇文章主要介紹了python遞歸&迭代方法實現(xiàn)鏈表反轉(zhuǎn),文章分享一段詳細實現(xiàn)代碼,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)或工作有所幫助
    2022-02-02
  • 對Python3.6 IDLE常用快捷鍵介紹

    對Python3.6 IDLE常用快捷鍵介紹

    今天小編就為大家分享一篇對Python3.6 IDLE常用快捷鍵介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python可變參數(shù)用法實例分析

    Python可變參數(shù)用法實例分析

    這篇文章主要介紹了Python可變參數(shù)用法,結(jié)合實例形式分析了Python可變參數(shù)的具體定義、使用方法與相關(guān)注意事項,需要的朋友可以參考下
    2017-04-04
  • Python入門教程(二十一)Python的數(shù)組

    Python入門教程(二十一)Python的數(shù)組

    這篇文章主要介紹了Python入門教程(二十一)Python的數(shù)組,數(shù)組是一種特殊變量,數(shù)組可以在單個名稱下保存多個值,我們可以通過引用索引號來訪問這些值,需要的朋友可以參考下
    2023-04-04

最新評論

海门市| 大关县| 金溪县| 堆龙德庆县| 临洮县| 凤庆县| 宜丰县| 泰州市| 英山县| 开原市| 荆门市| 稷山县| 衡山县| 富顺县| 邻水| 同德县| 泗水县| 永兴县| 靖西县| 黔西县| 乌鲁木齐县| 唐河县| 长沙县| 大田县| 泉州市| 娄烦县| 肃北| 伊宁市| 澳门| 宁津县| 濮阳县| 新巴尔虎左旗| 社旗县| 搜索| 延寿县| 商洛市| 内黄县| 东港市| 香格里拉县| 南江县| 西藏|