国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-01-04 17:58:07
字體:
來源:轉載
供稿:網(wǎng)友

本文是小編日常整理的些關于python執(zhí)行系統(tǒng)命令常見的方法,比較全面,特此通過武林網(wǎng)這個平臺把此篇文章分享給大家供大家參考

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

1 os.system

例如 ipython中運行如下命令,返回運行狀態(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.

運行返回結果

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 如何同時返回結果和運行狀態(tài),commands模塊:

 

 
  1. #String form: <module 'commands' from '/usr/lib64/python2.7/commands.pyc'> 
  2. File: /usr/lib64/python2.7/commands.py 
  3. Docstring: 
  4. Execute shell commands via os.popen() and return status, output. 
  5. Interface summary: 
  6. import commands 
  7. outtext = commands.getoutput(cmd) 
  8. (exitstatus, outtext) = commands.getstatusoutput(cmd) 
  9. outtext = commands.getstatus(file) # returns output of "ls -ld file" 
  10. A trailing newline is removed from the output string. 
  11. Encapsulates the basic operation: 
  12. pipe = os.popen('{ ' + cmd + '; } 2>&1''r'
  13. text = pipe.read() 
  14. 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 中運行"?subprocess"可以發(fā)現(xiàn)subprocess是python用來替換os.popen()等管道操作命令的新模塊

A more real-world example would look like this:

 

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

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

4.1.1 subprocess.call()

父進程父進程等待子進程完成,返回exit code

4.1.2 subprocess.check_call()

父進程等待子進程完成,返回0,如果returncode不為0,則舉出錯誤subprocess.CalledProcessError,該對象包含有returncode屬性,可用try...except...來檢查

4.1.3 subprocess.check_output()

父進程等待子進程完成

返回子進程向標準輸出的輸出結果

檢查退出信息,如果returncode不為0,則舉出錯誤subprocess.CalledProcessError,該對象包含有returncode屬性和output屬性,output屬性為標準輸出的輸出結果,可用try...except...來檢查

例如:

In [32]: out = subprocess.call("ls -l", shell=True)

total 42244

-rw-rw-r--. 1 *** *** 366 May 26 09:10 ChangeLog

4.2.1

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

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

 

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

加上wait方法:

 

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 玉环县| 大石桥市| 高碑店市| 自贡市| 鄄城县| 成都市| 东乡| 突泉县| 宜兰市| 库尔勒市| 类乌齐县| 壶关县| 邳州市| 桐乡市| 南昌市| 青阳县| 临汾市| 安达市| 青铜峡市| 安庆市| 揭西县| 新疆| 琼海市| 永嘉县| 三门峡市| 南投市| 银川市| 平南县| 旬邑县| 太白县| 天全县| 西充县| 长沙县| 宜兴市| 礼泉县| 佛学| 闻喜县| 武汉市| 威海市| 台江县| 拉萨市|