在Python中執(zhí)行系統(tǒng)命令的方法示例詳解
前言
Python經(jīng)常被稱作“膠水語言”,因為它能夠輕易地操作其他程序,輕易地包裝使用其他語言編寫的庫。在Python/wxPython環(huán)境下,執(zhí)行外部命令或者說在Python程序中啟動另一個程序的方法。
本文將詳細介紹關(guān)于Python中如何執(zhí)行系統(tǒng)命令的相關(guān)資料,下面話不多說了,來一起看看詳細的介紹吧。
(1) os.system()
這個方法直接調(diào)用標(biāo)準(zhǔn)C的system()
函數(shù),僅僅在一個子終端運行系統(tǒng)命令,而不能獲取執(zhí)行返回的信息。
>>> import os >>> output = os.system('cat /proc/cpuinfo') processor : 0 vendor_id : AuthenticAMD cpu family : 21 ... ... >>> output # doesn't capture output 0
(2) os.popen()
這個方法執(zhí)行命令并返回執(zhí)行后的信息對象,是通過一個管道文件將結(jié)果返回。
>>> output = os.popen('cat /proc/cpuinfo') >>> output <open file 'cat /proc/cpuinfo', mode 'r' at 0x7ff52d831540> >>> print output.read() processor : 0 vendor_id : AuthenticAMD cpu family : 21 ... ... >>><span style="font-size:14px;">
(3) commands模塊
>>> import commands >>> (status, output) = commands.getstatusoutput('cat /proc/cpuinfo') >>> print output processor : 0 vendor_id : AuthenticAMD cpu family : 21 ... ... >>> print status 0
注意1:在類unix的系統(tǒng)下使用此方法返回的返回值(status)與腳本或命令執(zhí)行之后的返回值不等,這是因為調(diào)用了os.wait()的緣故,具體原因就得去了解下系統(tǒng)wait()的實現(xiàn)了。需要正確的返回值(status),只需要對返回值進行右移8位操作就可以了。
注意2:當(dāng)執(zhí)行命令的參數(shù)或者返回中包含了中文文字,那么建議使用subprocess。
(4) subprocess模塊
該模塊是一個功能強大的子進程管理模塊,是替換os.system
, os.spawn*
等方法的一個模塊。
>>> import subprocess >>> subprocess.Popen(["ls", "-l"]) <strong> # python2.x</strong> doesn't capture output >>> subprocess.run(["ls", "-l"]) <strong># python3.x</strong> doesn't capture output <subprocess.Popen object at 0x7ff52d7ee490> >>> total 68 drwxrwxr-x 3 xl xl 4096 Feb 8 05:00 com drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Desktop drwxr-xr-x 2 xl xl 4096 Jan 21 02:58 Documents drwxr-xr-x 2 xl xl 4096 Jan 21 07:44 Downloads ... ... >>>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
補充:
最近在做那個測試框架的時候發(fā)現(xiàn) Python 的另一個獲得系統(tǒng)執(zhí)行命令的返回值和輸出的類。
最開始的時候用 Python 學(xué)會了 os.system() 這個方法是很多比如 C,Perl 相似的。
os.system('cat /proc/cpuinfo')
但是這樣是無法獲得到輸出和返回值的,繼續(xù) Google,之后學(xué)會了 os.popen()。
output = os.popen('cat /proc/cpuinfo')
print output.read()
通過 os.popen() 返回的是 file read 的對象,對其進行讀取 read() 的操作可以看到執(zhí)行的輸出。但是怎么讀取程序執(zhí)行的返回值呢,當(dāng)然咯繼續(xù)請教偉大的 Google(聯(lián)想到像我這樣的人工作如果離開了 Google,不是成了廢物。。。Baidu 忽視)。Google 給我指向了 commands — Utilities for running commands。
這樣通過 commands.getstatusoutput() 一個方法就可以獲得到返回值和輸出,非常好用。
(status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
print status, output
Python Document 中給的一個例子,很清楚的給出了各方法的返回。
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
相關(guān)文章
python 基于Appium控制多設(shè)備并行執(zhí)行
這篇文章主要介紹了python 如何基于Appium控制多設(shè)備并行執(zhí)行,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03Python實現(xiàn)Pig Latin小游戲?qū)嵗a
這篇文章主要介紹了Python實現(xiàn)Pig Latin小游戲?qū)嵗a,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02