pip" />

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 2182 0

具體內(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: 
            
              
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
          

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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲精品国产精品一区二区 | 国产一区二区三区 | 日韩欧美中文字幕在线播放 | 美日韩毛片 | 青草青草久热精品视频99 | 99久久精品国产高清一区二区 | 四虎影视在线影院4hutv | 久久99深爱久久99精品 | 99精品在线免费观看 | 亚洲在线播放 | 日韩欧美天堂 | 欧美亚洲国产精品久久高清 | 日韩视频大全 | 成人免费黄色 | 欧美中文综合在线视频 | 日韩色中色| 99伦理| 久久免费视频在线观看30 | 亚洲第一永久在线观看 | 中国妞xxxx| 精品综合久久久久久97超人 | 福利视频免费观看 | 欧美亚洲精品一区 | 97久久精品人人澡人人爽 | 精品久久久久久乐 | 亚洲综合区小说区激情区噜噜 | 亚洲欧美国产精品久久久 | 免费a黄色 | 久久综合九色婷婷97 | 欧美三级做爰在线 | 亚洲国产精品综合久久网络 | 98在线视频噜噜噜国产 | 四虎影视永久免费观看网址 | 亚洲精品视频久久久 | 成人yyyy | 中文字幕在线不卡精品视频99 | 成人亚洲欧美日韩中文字幕 | 91青娱国产| 久久se精品动漫一区二区三区 | 国产精品美女一区二区三区 | 久久国产精品岛国搬运工 |