調用shell

1. os 模塊


1.1. os模塊的exec方法族

Python的exec系統方法同Unix的exec系統調用是一致的。這些方法適用於在子進程中調用外部程序的情況,因爲外部程序會替換當前進程的代碼,不會返回。( 這個看了點 help(os) --> search "exec" 的相關介紹,但是沒太搞明白咋使用)



1.2. os模塊的system方法


system方法會創建子進程運行外部程序,方法只返回外部程序的運行結果。這個方法比較適用於外部程序沒有輸出結果的情況。

>>> 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方法

當需要得到外部程序的輸出結果時,本方法非常有用。比如使用urllib調用Web API時,需要對得到的數據進行處理。os.popen(cmd) 要得到命令的輸出內容,只需再調用下read()或readlines()等 如a=os.popen(cmd).read()

>>> 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

</open>



2. commands 模塊


使用commands模塊的getoutput方法,這種方法同popend的區別在於popen返回的是一個文件句柄,而本方法將外部程序的輸出結果當作字符串返回,很多情況下用起來要更方便些。

主要方法:


* commands.getstatusoutput(cmd) 返回(status, output)

* commands.getoutput(cmd) 只返回輸出結果

* commands.getstatus(file) 返回ls -ld file的執行結果字符串,調用了getoutput,不建議使用此方法

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啓動子進程來幹活。

>>> 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  os.popen2 os.popen3

使用stream = os.popen("some_command with args")也能做與os.system一樣的事,與os.system不同的是os.popen會給你一個像文件的對象從而你可以使用它來訪問哪個程序的標準輸入、輸出。而且popen還有三個變種都是在I/O處理上有輕微不同。假如你通過一個字符串傳遞所有東西,你的命令會傳遞給shell;如果你通過一個列表傳遞他們,你不用擔心逃避任何事。


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可能更加適合你。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章