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

python中subprocess批量執(zhí)行l(wèi)inux命令

 更新時(shí)間:2018年04月27日 08:43:35   投稿:laozhang  
本篇文章給大家詳細(xì)講述了python中使用subprocess批量執(zhí)行l(wèi)inux命令的方法,有興趣的朋友參考學(xué)習(xí)下。

可以執(zhí)行shell命令的相關(guān)模塊和函數(shù)有:

  • os.system
  • os.spawn
  • os.popen --廢棄
  • popen --廢棄
  • commands --廢棄,3.x中被移除

以上執(zhí)行shell命令的相關(guān)的模塊和函數(shù)的功能均在 subprocess 模塊中實(shí)現(xiàn),并提供了更豐富的功能。

subprocess

call

執(zhí)行命令,返回狀態(tài)碼

>>> import subprocess
>>> ret = subprocess.call(["ls", "-l"], shell=False)
total 4684
-rw-r--r-- 1 root root   454 May 5 12:20 aa.py
-rw-r--r-- 1 root root    0 May 8 16:51 aa.txt
-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe
-rw-r--r-- 1 root root   422 May 5 12:20 ip_info.txt
-rw-r--r-- 1 root root   718 Apr 19 10:52 my.cnf
>>> ret = subprocess.call("ls -l", shell=True)
total 4684
-rw-r--r-- 1 root root   454 May 5 12:20 aa.py
-rw-r--r-- 1 root root    0 May 8 16:51 aa.txt
-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe
-rw-r--r-- 1 root root   422 May 5 12:20 ip_info.txt
-rw-r--r-- 1 root root   718 Apr 19 10:52 my.cnf
>>> print(ret)
0

check_call

執(zhí)行命令,如果執(zhí)行狀態(tài)碼是 0 ,則返回0,否則拋異常

>>> subprocess.check_call(["ls", "-l"])
total 4684
-rw-r--r-- 1 root root   454 May 5 12:20 aa.py
-rw-r--r-- 1 root root    0 May 8 16:51 aa.txt
-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe
-rw-r--r-- 1 root root   422 May 5 12:20 ip_info.txt
-rw-r--r-- 1 root root   718 Apr 19 10:52 my.cnf
0
>>> subprocess.check_call("exit 1", shell=True)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 581, in check_call
  raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

check_output

執(zhí)行命令,如果狀態(tài)碼是 0 ,則返回執(zhí)行結(jié)果,否則拋異常

>>> subprocess.check_output(["echo", "Hello World!"])
b'Hello World!\n'
>>> subprocess.check_output("exit 1", shell=True)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 626, in check_output
  **kwargs).stdout
 File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 708, in run
  output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

subprocess.Popen(...)

用于執(zhí)行復(fù)雜的系統(tǒng)命令

參數(shù):

args:shell命令,可以是字符串或者序列類型(如:list,元組)

bufsize:指定緩沖。0 無緩沖,1 行緩沖,其他 緩沖區(qū)大小,負(fù)值 系統(tǒng)緩沖

stdin, stdout, stderr:分別表示程序的標(biāo)準(zhǔn)輸入、輸出、錯(cuò)誤句柄

preexec_fn:只在Unix平臺(tái)下有效,用于指定一個(gè)可執(zhí)行對象(callable object),它將在子進(jìn)程運(yùn)行之前被調(diào)用

close_sfs:在windows平臺(tái)下,如果close_fds被設(shè)置為True,則新創(chuàng)建的子進(jìn)程將不會(huì)繼承父進(jìn)程的輸入、輸出、錯(cuò)誤管道。

所以不能將close_fds設(shè)置為True同時(shí)重定向子進(jìn)程的標(biāo)準(zhǔn)輸入、輸出與錯(cuò)誤(stdin, stdout, stderr)。

shell:同上

cwd:用于設(shè)置子進(jìn)程的當(dāng)前目錄

env:用于指定子進(jìn)程的環(huán)境變量。如果env = None,子進(jìn)程的環(huán)境變量將從父進(jìn)程中繼承。

universal_newlines:不同系統(tǒng)的換行符不同,True -> 同意使用 n

startupinfo與createionflags只在windows下有效

將被傳遞給底層的CreateProcess()函數(shù),用于設(shè)置子進(jìn)程的一些屬性,如:主窗口的外觀,進(jìn)程的優(yōu)先級等等

執(zhí)行普通命令

>>> import subprocess
>>> ret1 = subprocess.Popen(["mkdir","t1"])
>>> ret2 = subprocess.Popen("mkdir t2", shell=True)
>>> print(ret1)
<subprocess.Popen object at 0x7f4d7609dd30>
>>> print(ret2)
<subprocess.Popen object at 0x7f4d7609dc18>

終端輸入的命令分為兩種:

  • 輸入即可得到輸出,如:ifconfig
  • 輸入進(jìn)行某環(huán)境,依賴再輸入,如:python
>>> import subprocess
>>> obj = subprocess.Popen("mkdir t3", shell=True, cwd='/tmp/',)
>>> import subprocess
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> obj.stdin.write("print(1)\n")
9
>>> obj.stdin.write("print(2)")
8
>>> obj.stdin.close()
>>> cmd_out = obj.stdout.read()
>>> obj.stdout.close()
>>> cmd_error = obj.stderr.read()
>>> obj.stderr.close()
>>> print(cmd_out)
1
2
>>> print(cmd_error)
>>> import subprocess
>>> 
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> obj.stdin.write("print(1)\n")
9
>>> obj.stdin.write("print(2)")
8
>>> 
>>> out_error_list = obj.communicate()
>>> print(out_error_list)
('1\n2\n', '')
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> out_error_list = obj.communicate('print("hello")')
>>> print(out_error_list)
('hello\n', '')

相關(guān)文章

  • Python函數(shù)的迭代器與生成器的示例代碼

    Python函數(shù)的迭代器與生成器的示例代碼

    這篇文章主要介紹了Python函數(shù)的迭代器與生成器的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • pymongo中g(shù)roup by的操作方法教程

    pymongo中g(shù)roup by的操作方法教程

    這篇文章主要給大家介紹了關(guān)于pymongo中g(shù)roup by的操作方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用pymongo具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • python實(shí)現(xiàn)用戶答題功能

    python實(shí)現(xiàn)用戶答題功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)用戶答題功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Python入門基礎(chǔ)之?dāng)?shù)字字符串與列表

    Python入門基礎(chǔ)之?dāng)?shù)字字符串與列表

    這篇文章主要給大家介紹了關(guān)于Python入門基礎(chǔ)之?dāng)?shù)字字符串與列表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python利用pandas和matplotlib實(shí)現(xiàn)繪制圓環(huán)圖

    Python利用pandas和matplotlib實(shí)現(xiàn)繪制圓環(huán)圖

    在可視化的過程中,圓環(huán)圖是一種常用的方式,特別適合于展示各類別占比情況,本文將介紹如何使用 Python中的 pandas 和 matplotlib 庫,來制作一個(gè)店鋪銷量占比的圓環(huán)圖,需要的可以參考下
    2023-11-11
  • Python初識(shí)邏輯與if語句及用法大全

    Python初識(shí)邏輯與if語句及用法大全

    這篇文章主要介紹了Python初識(shí)邏輯與if語句,文中給大家提到了if語句功能及用法講解,需要的朋友可以參考下
    2021-08-08
  • Python自動(dòng)化辦公之合并多個(gè)Excel

    Python自動(dòng)化辦公之合并多個(gè)Excel

    在日常的辦公自動(dòng)化工作中,尤其是處理大量數(shù)據(jù)時(shí),合并多個(gè)?Excel?表格是一個(gè)常見且繁瑣的任務(wù),下面小編就來為大家介紹一下如何使用Python輕松實(shí)現(xiàn)合并多個(gè)Excel吧
    2025-02-02
  • Python 支付整合開發(fā)包的實(shí)現(xiàn)

    Python 支付整合開發(fā)包的實(shí)現(xiàn)

    這篇文章主要介紹了Python 支付整合開發(fā)包的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • 將python安裝信息加入注冊表的示例

    將python安裝信息加入注冊表的示例

    今天小編就為大家分享一篇將python安裝信息加入注冊表的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python操作列表之List.insert()方法的使用

    Python操作列表之List.insert()方法的使用

    這篇文章主要介紹了Python操作列表之List.insert()方法的使用,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05

最新評論

含山县| 和田县| 红原县| 汾阳市| 海林市| 建德市| 新余市| 揭东县| 罗源县| 柳河县| 靖州| 大厂| 宁明县| 莱州市| 吉安市| 福泉市| 全州县| 赤峰市| 平塘县| 西华县| 呼图壁县| 寻甸| 合川市| 高州市| 沭阳县| 平塘县| 阿鲁科尔沁旗| 茶陵县| 上林县| 新闻| 上高县| 巴塘县| 洞头县| 嘉鱼县| 桃园市| 建阳市| 洱源县| 大余县| 阳西县| 永胜县| 新乐市|