有時候難免需要直接調用Shell命令來完成一些比較簡單的操作,比如mount一個檔案系統之類的。那麼我們使用Python如何調用Linux的Shell命令?下面來介紹幾種常用的方法:
1. os 模組
1.1. os模組的exec方法族
Python的exec系統方法同Unix的exec系統調用是一致的。這些方法適用 於在子進程中調用外部程式的情況,因為外部程式會替換當前進程的代碼,不會返回。( 這個看了點 help(os) --> search "exec" 的相關介紹,但是沒太搞明白咋使用)
1.2. os模組的system方法
system方法會建立子進程運行外部程式,方法只返回外部程式的運行結果。這個方法比較適用於外部程式沒有輸出結果的情況。
[python] view plaincopy
>>> import os
>>> os.system("echo \"Hello World\"") # 直接使用os.system調用一個echo命令
Hello World ——————> 列印命令結果
0 ——————> What's this ? 傳回值?
>>> val = os.system("ls -al | grep \"log\" ") # 使用val接收傳回值
-rw-r--r-- 1 root root 6030829 Dec 31 15:14 log ——————> 此時只列印了命令結果
>>> print val
0 ——————> 注意,此時命令正常運行時,傳回值是0
>>> val = os.system("ls -al | grep \"log1\" ")
>>> print val
256 ——————> 使用os.system調用一個沒有返回結果的命令,傳回值為256~
>>>
注意:上面說了,此方法脂肪會外部程式的結果,也就是os.system的結果,所以如果你想接收命令的傳回值,接著向下看~
1.3. os模組的popen方法
當需要得到外部程式的輸出結果時,本方法非常有用,返回一個類檔案對象,調用該對象的read()或readlines()方法可以讀取輸出內容。比如使用urllib調用Web API時,需要對得到的資料進行處理。os.popen(cmd) 要得到命令的輸出內容,只需再調用下read()或readlines()等 如a=os.popen(cmd).read()
[python] view plaincopy
>>> os.popen('ls -lt') # 調用os.popen(cmd)並不能得到我們想要的結果
<open file 'ls -lt ', mode 'r' at 0xb7585ee8>
>>> print os.popen('ls -lt').read() # 調用read()方法可以得到命令的結果
total 6064
-rwxr-xr-x 1 long long 23 Jan 5 21:00 hello.sh
-rw-r--r-- 1 long long 147 Jan 5 20:26 Makefile
drwxr-xr-x 3 long long 4096 Jan 2 19:37 test
-rw-r--r-- 1 root root 6030829 Dec 31 15:14 log
drwxr-xr-x 2 long long 4096 Dec 28 09:36 pip_build_long
drwx------ 2 Debian-gdm Debian-gdm 4096 Dec 23 19:08 pulse-gylJ5EL24GU9
drwx------ 2 long long 4096 Jan 1 1970 orbit-long
>>> val = os.popen('ls -lt').read() # 使用變數可以接收命令傳回值
>>> if "log" in val: # 我們可以使用in來判斷傳回值中有木有一個字串
... print "Haha,there is the log"
... else:
... print "No,not happy"
...
Haha,there is the log
2. commands 模組
使用commands模組的getoutput方法,這種方法同popend的區別在於popen返回的是一個類檔案對象,而本方法將外部程式的輸出結果當作字串返回,很多情況下用起來要更方便些。
主要方法:
* commands.getstatusoutput(cmd) 返回(status, output)
* commands.getoutput(cmd) 只返回輸出結果
* commands.getstatus(file) 返回ls -ld file的執行結果字串,調用了getoutput,不建議使用此方法
[python] view plaincopy
long@zhouyl:/tmp/tests$ python
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import commands
>>> commands.getstatusoutput('ls -lt') # 返回(status, output)
(0, 'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log')
>>> commands.getoutput('ls -lt') # 返回命令的輸出結果(貌似和Shell命令的輸出格式不同哈~)
'total 5900\n-rwxr-xr-x 1 long long 23 Jan 5 21:34 hello.sh\n-rw-r--r-- 1 long long 147 Jan 5 21:34 Makefile\n-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
>>> commands.getstatus('log') # 調用commands.getoutput中的命令對'log'檔案進行相同的操作
'-rw-r--r-- 1 long long 6030829 Jan 5 21:34 log'
>>>
3. subprocess模組
根據Python官方文檔說明,subprocess模組用於取代上面這些模組。有一個用Python實現的並行ssh工具—mssh,代碼很簡短,不過很有意思,它線上程中調用subprocess啟動子進程來幹活。
[python] view plaincopy
>>> from subprocess import call
>>> call(["ls", "-l"])
subprocess與system相比的優勢是它更靈活(你可以得到標準輸出,標準錯誤,“真正”的狀態碼,更好的錯誤處理,等..)。我認為使用os.system已淘汰,或即將過時。
4. 眾方法的比較以及總結
4.1. 關於 os.system
os.system("some_command with args")將命令以及參數傳遞給你的系統shell,這很好,因為你可以用這種方法同時運行多個命令並且可以設定管道以及輸入輸出重新導向。比如:
os.system("some_command < input_file | another_command > output_file")
然而,雖然這很方便,但是你需要手動處理shell字元的轉義,比如空格等。此外,這也只能讓你運行簡單的shell命令而且不能運行外部程式。
4.2. 關於os.popen
使用stream = os.popen("some_command with args")也能做與os.system一樣的事,與os.system不同的是os.popen會返回一個類檔案對象,使用它來訪問標準輸入、輸出。
4.3. 關於subprocess.popen
subprocess模組的Popen類,意圖作為os.popen的替代,但是因為其很全面所以比os.popen要顯得稍微複雜。
比如你可以使用 print Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read() 來替代 print os.popen("echo Hello World").read()。但是相比之下它使用一個統一的類包括4中不同的popen函數還是不錯的。
4.4. 關於subprocess.call
subprocess模組的call函數。它基本上就像Popen類並都使用相同的參數,但是它只簡單的等待命令完成並給你傳回碼。比如:
return_code = subprocess.call("echo Hello World", shell=True)
os模組中還有C中那樣的fork/exec/spawn函數,但是我不建議直接使用它們。subprocess可能更加適合你。
===========================================
[1] http://demi-panda.com/2013/01/25/python-shell-command/index.html
[2] http://m.blog.csdn.net/blog/overstack/9295995
[3] http://blog.csdn.net/swiftshow/article/details/7755543
下面是對於文中所涉及的內容的python官方文檔:
[4] http://docs.python.org/library/subprocess.html#replacing-older-functions-with-the-subprocess-module -- 關於使用subprocess 替代老的方法
[5] http://docs.python.org/lib/os-process.html -- os的exec方法族以及system方法
[6] http://docs.python.org/lib/os-newstreams.html -- os的popen方法
[7] http://docs.python.org/lib/node528.html -- os的subprocess介紹