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

日常整理python執(zhí)行系統(tǒng)命令的常見(jiàn)方法(全)

 更新時(shí)間:2015年10月22日 16:39:28   投稿:mrr  
本文是小編日常整理的些關(guān)于python執(zhí)行系統(tǒng)命令常見(jiàn)的方法,比較全面,特此通過(guò)腳本之家這個(gè)平臺(tái)把此篇文章分享給大家供大家參考

具體內(nèi)容如下:

1 os.system

例如 ipython中運(yùn)行如下命令,返回運(yùn)行狀態(tài)status

os.system('cat /etc/passwdqc.conf')
min=disabled,24,11,8,7
max=40
passphrase=3
match=4
similar=deny
random=47
enforce=everyone
retry=3
Out[6]: 0

2 os.popen()

popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.

運(yùn)行返回結(jié)果

In [20]: output = os.popen('cat /proc/cpuinfo')
In [21]: lineLen = []
In [22]: for line in output.readlines():
    lineLen.append(len(line))
   ....:    
In [23]: line
line     lineLen 
In [23]: lineLen
Out[23]:
[14,
 25,
...

3 如何同時(shí)返回結(jié)果和運(yùn)行狀態(tài),commands模塊:

#String form: <module 'commands' from '/usr/lib64/python2.7/commands.pyc'>
File: /usr/lib64/python2.7/commands.py
Docstring:
Execute shell commands via os.popen() and return status, output.
Interface summary:
import commands
outtext = commands.getoutput(cmd)
(exitstatus, outtext) = commands.getstatusoutput(cmd)
outtext = commands.getstatus(file) # returns output of "ls -ld file"
A trailing newline is removed from the output string.
Encapsulates the basic operation:
pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
text = pipe.read()
sts = pipe.close()

commands示例如下:

In [24]: (status, output) = commands.getstatusoutput('cat /proc/cpuinfo')
In [25]: status
Out[25]: 0
In [26]: len(output)
Out[26]: 3859

4 使用模塊subprocess

ipython 中運(yùn)行"?subprocess"可以發(fā)現(xiàn)subprocess是python用來(lái)替換os.popen()等管道操作命令的新模塊

A more real-world example would look like this:

try:
 retcode = call("mycmd" + " myarg", shell=True)
 if retcode < 0:
  print >>sys.stderr, "Child was terminated by signal", -retcode
 else:
  print >>sys.stderr, "Child returned", retcode
except OSError, e:
 print >>sys.stderr, "Execution failed:", e

相對(duì)于上面幾種方式,subprocess便于控制和監(jiān)控進(jìn)程運(yùn)行結(jié)果,subprocess提供多種函數(shù)便于應(yīng)對(duì)父進(jìn)程對(duì)子進(jìn)程不同要求:

4.1.1 subprocess.call()

父進(jìn)程父進(jìn)程等待子進(jìn)程完成,返回exit code

4.1.2 subprocess.check_call()

父進(jìn)程等待子進(jìn)程完成,返回0,如果returncode不為0,則舉出錯(cuò)誤subprocess.CalledProcessError,該對(duì)象包含有returncode屬性,可用try...except...來(lái)檢查

4.1.3 subprocess.check_output()

父進(jìn)程等待子進(jìn)程完成

返回子進(jìn)程向標(biāo)準(zhǔn)輸出的輸出結(jié)果

檢查退出信息,如果returncode不為0,則舉出錯(cuò)誤subprocess.CalledProcessError,該對(duì)象包含有returncode屬性和output屬性,output屬性為標(biāo)準(zhǔn)輸出的輸出結(jié)果,可用try...except...來(lái)檢查

例如:

In [32]: out = subprocess.call("ls -l", shell=True)
total 42244
-rw-rw-r--.  1 *** ***     366 May 26 09:10 ChangeLog

4.2.1

上面三個(gè)函數(shù)都是源于Popen()函數(shù)的wapper(封裝),如果需要更加個(gè)性化應(yīng)用,那么就需要使用popen()函數(shù)

Popen對(duì)象創(chuàng)建后,主程序不會(huì)自動(dòng)等待子進(jìn)程完成。我們必須調(diào)用對(duì)象的wait()方法,父進(jìn)程才會(huì)等待 (也就是阻塞block)

[wenwt@localhost syntax]$ rm subprocess.pyc 
[wenwt@localhost syntax]$ python process.py 
parent process
[wenwt@localhost syntax]$ PING www.google.com (173.194.219.99) 56(84) bytes of data.
^C
[wenwt@localhost syntax]$ 
--- www.google.com ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 3999ms

加上wait方法:

[wenwt@localhost syntax]$ python process.py 
PING www.google.com (173.194.219.103) 56(84) bytes of data.
--- www.google.com ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 3999ms
parent process

補(bǔ)充介紹:Python 執(zhí)行終端命令的方法

import os
import subprocess
'''
os.system模塊
os.system("ls -hl") 執(zhí)行命令并返回狀態(tài)碼,當(dāng)返回0表示成功;返回256表示失敗,痛點(diǎn)是無(wú)法返回output
os.popen模塊
os.popen("ls -hl") 執(zhí)行命令,之后通過(guò).read()方法獲取output返回值
subprocess模塊
subprocess.getstatusoutput("ls -hl") 執(zhí)行命令,并返回狀態(tài)status、輸出output
subprocess.getoutput("ls -hl")    執(zhí)行命令,只返回輸出結(jié)果output
subprocess.call("ls -hl")      執(zhí)行命令并返回狀態(tài)碼 和os.system("ls -hl")類似
'''
def test_system(cmd):
  status = os.system(cmd) # 會(huì)自動(dòng)輸出output到控制臺(tái) 但是無(wú)法接收,status為0表示成功、status為256表示失敗
  print(status)
def test_popen(cmd):
  output = os.popen(cmd).read() # 只會(huì)獲取到命令的output,如果是有output的錯(cuò)誤命令 會(huì)輸出output,否則輸出空白
  print(output)
def test_getoutput(cmd):
  output = subprocess.getoutput(cmd) # 和os.popen(cmd)類似
  print(output)
def test_getstatusoutput(cmd):
  status, output = subprocess.getstatusoutput(cmd) # 執(zhí)行命令,并返回狀態(tài)status、輸出output
  print(status)
  print(output)
def test_call(cmd):
  status = subprocess.call(cmd) # 和os.system(cmd)類似
  print(status)
if __name__ == '__main__':
  # test_system('ls -lh') # test_system('test')
  # test_popen('pwd') # test_popen('test')
  # test_getoutput('pwd')
  # test_getstatusoutput('pwd')
  test_call('pwd')

以上內(nèi)容就是本文的全部敘述,希望大家喜歡。

相關(guān)文章

  • python paramiko實(shí)現(xiàn)ssh遠(yuǎn)程訪問(wèn)的方法

    python paramiko實(shí)現(xiàn)ssh遠(yuǎn)程訪問(wèn)的方法

    這篇文章主要介紹了python paramiko模塊實(shí)現(xiàn)ssh遠(yuǎn)程訪問(wèn)的方法,大家參考使用
    2013-12-12
  • Python實(shí)現(xiàn)語(yǔ)音識(shí)別vosk的示例代碼

    Python實(shí)現(xiàn)語(yǔ)音識(shí)別vosk的示例代碼

    Vosk是一個(gè)功能強(qiáng)大且易于使用的語(yǔ)音識(shí)別工具包,它提供了Python綁定,使得在Python中使用Vosk變得非常方便,本文主要介紹了Python實(shí)現(xiàn)語(yǔ)音識(shí)別vosk的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • python實(shí)現(xiàn)連連看輔助(圖像識(shí)別)

    python實(shí)現(xiàn)連連看輔助(圖像識(shí)別)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)連連看輔助程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Python中循環(huán)依賴問(wèn)題及其解決方案

    Python中循環(huán)依賴問(wèn)題及其解決方案

    在軟件開(kāi)發(fā)中,循環(huán)依賴是一個(gè)常見(jiàn)的問(wèn)題,尤其是在使用 Python 這樣的動(dòng)態(tài)語(yǔ)言時(shí),循環(huán)依賴指的是兩個(gè)或多個(gè)模塊或組件相互依賴,形成一個(gè)閉環(huán),本文將探討 Python 中循環(huán)依賴的問(wèn)題,并提供一些解決方案,需要的朋友可以參考下
    2024-06-06
  • Python使用selenium + headless chrome獲取網(wǎng)頁(yè)內(nèi)容的方法示例

    Python使用selenium + headless chrome獲取網(wǎng)頁(yè)內(nèi)容的方法示例

    這篇文章主要介紹了Python使用selenium + headless chrome獲取網(wǎng)頁(yè)內(nèi)容的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲

    Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • pycharm執(zhí)行python時(shí),填寫(xiě)參數(shù)的方法

    pycharm執(zhí)行python時(shí),填寫(xiě)參數(shù)的方法

    今天小編就為大家分享一篇pycharm執(zhí)行python時(shí),填寫(xiě)參數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 一文了解Python?流程控制

    一文了解Python?流程控制

    這篇文章主要介紹了一文了解Python?流程控制,Python?中有while和for兩種循環(huán)機(jī)制,其中while循環(huán)是條件循環(huán),文章通過(guò)展開(kāi)循環(huán)內(nèi)容展開(kāi)控制流程詳情,需要的小伙伴可以參考一下
    2022-05-05
  • Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序

    Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序

    這篇文章主要介紹了Python實(shí)現(xiàn)的檢測(cè)web服務(wù)器健康狀況的小程序,本文使用socket庫(kù)來(lái)實(shí)現(xiàn),需要的朋友可以參考下
    2014-09-09
  • Django框架實(shí)現(xiàn)的分頁(yè)demo示例

    Django框架實(shí)現(xiàn)的分頁(yè)demo示例

    這篇文章主要介紹了Django框架實(shí)現(xiàn)的分頁(yè)demo,結(jié)合實(shí)例形式分析了Django框架分頁(yè)的步驟、原理、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2019-05-05

最新評(píng)論

滁州市| 阜城县| 武夷山市| 屏东市| 射洪县| 阜新市| 云阳县| 华阴市| 大丰市| 微博| 星子县| 临湘市| 桃源县| 明星| 临桂县| 织金县| 大竹县| 景德镇市| 武清区| 泽州县| 宁安市| 墨江| 翼城县| 格尔木市| 布尔津县| 瑞安市| 时尚| 汉源县| 寿阳县| 新兴县| 荣昌县| 克拉玛依市| 资中县| 东乡族自治县| 武宁县| 自贡市| 日土县| 高阳县| 北海市| 灵石县| 大港区|