日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python使用shell命令_python 调用shell命令的方法

發布時間:2024/7/19 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python使用shell命令_python 调用shell命令的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在python程序中調用shell命令,是件很酷且常用的事情……

1. os.system(command)

此函數會啟動子進程,在子進程中執行command,并返回command命令執行完畢后的退出狀態,如果command有執行內容,會在標準輸出顯示。這實際上是使用C標準庫函數system()實現的。

缺點:這個函數在執行command命令時需要重新打開一個終端,并且無法保存command命令的執行結果。

實例:os.system('ls -l *')

2. os.popen(command,mode)

打開一個與command進程之間的管道。這個函數的返回值是一個文件對象,可以讀或者寫(由mode決定,mode默認是’r')。如果mode為’r',可以使用此函數的返回值調用read()來獲取command命令的執行結果。

os.system(cmd)或os.popen(cmd),前者返回值是腳本的退出狀態碼,后者的返回值是腳本執行過程中的輸出內容。實際使用時視需求情況而選擇。

實例:tmp = os.popen('ls -l *').readlines()

3. commands.getstatusoutput(command)

使用os.popen()函數執行command命令并返回一個元組(status,output),分別表示command命令執行的返回狀態和執行結果。對command的執行實際上是按照{command;} 2>&1的方式,所以output中包含控制臺輸出信息或者錯誤信息。output中不包含尾部的換行符。

4. subprocess模塊

此模塊在python2.4中初次亮相,其中集中了關于進程的諸多操作,其中的call()完全替代了system(),而popen()被更為豐富的Popen類替代;

總結:python提供了十分完善的調用shell命令的功能,在實戰中,我碰到的問題,有system和popen基本可全部搞定;

1 os.system

可以返回運行shell命令狀態,同時會在終端輸出運行結果

例如 ipython中運行如下命令,返回運行狀態status

os.system('cat /etc/passwdqc.conf')

min=disabled,24,11,8,7max=40passphrase=3match=4similar=deny

random=47enforce=everyone

retry=3Out[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 inoutput.readlines():

lineLen.append(len(line))

....:

In [23]: line

line lineLen

In [23]: lineLen

Out[23]:

[14,

25,

...

3 如何同時返回結果和運行狀態,commands模塊:

#String form:

File: /usr/lib64/python2.7/commands.py

Docstring:

Execute shell commands via os.popen() and returnstatus, output.

Interface summary:

importcommands

outtext =commands.getoutput(cmd)

(exitstatus, outtext) =commands.getstatusoutput(cmd)

outtext = commands.getstatus(file) #returns output of "ls -ld file"

A trailing newline is removed fromthe 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

通常項目中經常使用方法為subporcess.Popen, 我們可以在Popen()建立子進程的時候改變標準輸入、標準輸出和標準錯誤,并可以利用subprocess.PIPE將多個子進程的輸入和輸出連接在一起,構成管道(pipe):

importsubprocess

child1 = subprocess.Popen(["ls","-l"], stdout=subprocess.PIPE)

child2 = subprocess.Popen(["wc"], stdin=child1.stdout,stdout=subprocess.PIPE)

out =child2.communicate()

print(out)

在例如使用lsblk查看swap分區的uuid:

importsubprocess

child = subprocess.Popen(["lsblk", "-f"], stdout=subprocess.PIPE)

out =child.stdout.readlines()

swap_uuid =None

for item inout:

line =item.strip().split()

if len(line) == 4:

if(line[1] == 'swap'):

swap_uuid = line[2]

print(swap_uuid)

ipython 中運行"?subprocess"可以發現subprocess是python用來替換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

exceptOSError, e:

print >>sys.stderr, "Execution failed:", e

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

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

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

Popen對象創建后,主程序不會自動等待子進程完成。我們必須調用對象的wait()方法,父進程才會等待 (也就是阻塞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

總結

以上是生活随笔為你收集整理的python使用shell命令_python 调用shell命令的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。