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

歡迎訪問 生活随笔!

生活随笔

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

python

python调用shell命令之三慷慨法

發布時間:2025/3/20 python 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python调用shell命令之三慷慨法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

preface: 忙于近期的任務,須要用到libsvm的一些命令。如在終端執行javasvm_train train_file model_file. pythonsubset.py file train_num train_file test_file等命令。但file的準備又是通過python寫好的。file須要是libsvm可以接受的格式。故用python寫好特征。轉為libsvm可以接受的格式。生成file,然后在終端調用訓練。故想著直接在python代碼里面直接執行終端的命令。

#=====================================================

一、os 模塊

1.1.os模塊的exec方法簇:

ipython交互界面中:

In [1]: import osIn [2]: os.exec os.execl os.execlp os.execv os.execvp os.execle os.execlpe os.execve os.execvpe In [2]: os.execl? Type: function String form: <function execl at 0xb73673e4> File: /home/shifeng/anaconda/lib/python2.7/os.py Definition: os.execl(file, *args) Docstring: execl(file, *args)Execute the executable file with argument list args, replacing the current process.

os.exec+Tab鍵智能提示能夠看到有8個,help(os.execl)等能夠找到其使用方法說明。

1.2.os模塊的system方法

system方法會創建子進程執行外部程序。方法僅僅返回外部程序的執行結果。0表示執行成功。

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [10]: os.system("echo \"hello world\"") hello world Out[10]: 0In [11]: os.system("ls") all_roc_plot.py~ task1_feature_all2.py test.py test.sh ui_without_buy~ scrapy_work test test.py~ test.sh~ Out[11]: 0In [12]: os.system("cat test.sh") echo "hello world" Out[12]: 0In [13]: os.system("sh test.sh") hello world Out[13]: 0

如上。一些主要的shell命令,傳入os.system()參數里面,便能運行。

只是無法得到命令的返回值。

1.3.os模塊popen方法

popen方法可以得到shell命令的返回值。os.popen(cmd)后,須要再調用read()或者readlines()這兩個命令。輸出結果。

In [14]: os.popen("ls") Out[14]: <open file 'ls', mode 'r' at 0xb67efd30>In [15]: os.popen("ls").read() Out[15]: 'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~\n'In [16]: os.popen("ls").readlines() Out[16]: ['all_roc_plot.py~\n','scrapy_work\n','task1_feature_all2.py\n','test\n','test.py\n','test.py~\n','test.sh\n','test.sh~\n','ui_without_buy~\n']In [17]: s = os.popen("ls").read()In [18]: for i in s.split("\n"):....: print i....: all_roc_plot.py~ scrapy_work task1_feature_all2.py test test.py test.py~ test.sh test.sh~ ui_without_buy~

注意。read()或者readlines()后,其每一個元素包括一個回車符\n。

二、commands模塊

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

  • commands.getstatusoutput(cmd) 返回(status, output)
  • commands.getoutput(cmd) 僅僅返回輸出結果
  • commands.getstatus(file) 返回ls -ld file的運行結果字符串,調用了getoutput。不建議使用此方法
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [8]: import commands In [9]: commands.getoutput("ls") Out[9]: 'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~'In [10]: commands.getstatusoutput("ls") Out[10]: (0,'all_roc_plot.py~\nscrapy_work\ntask1_feature_all2.py\ntest\ntest.py\ntest.py~\ntest.sh\ntest.sh~\nui_without_buy~')

三、subprocess模塊

使用subprocess模塊能夠創建新的進程。能夠與新建進程的輸入/輸出/錯誤管道連通。并能夠獲得新建進程運行的返回狀態。使用subprocess模塊的目的是替代os.system()、os.popen*()、commands.*等舊的函數或模塊。

3.1.subprocess.call([“some_command”,“some_argument”,“another_argument_or_path”])

subprocess.call(command,shell=True)

3.2.subprocess.Popen(command,shell=True)
假設command不是一個可運行文件。shell=True不可省。
使用subprocess模塊能夠創建新的進程,能夠與新建進程的輸入/輸出/錯誤管道連通。并能夠獲得新建進程運行的返回狀態。使用subprocess模塊的目的是替代os.system()、os.popen*()、commands.*等舊的函數或模塊。
最簡單的方法是使用classsubprocess.Popen(command,shell=True)。Popen類Popen.stdin,Popen.stdout,Popen.stderr三個實用的屬性,能夠實現與子進程的通信。

In [11]: from subprocess import callIn [12]: call(["ls","-l"])

總用量 40

-rw-rw-r-- 1 shifeng shifeng 3266 53 14:14 all_roc_plot.py~ drwxrwxr-x 6 shifeng shifeng 4096 618 16:59 scrapy_work -rw-rw-r-- 1 shifeng shifeng 12786 610 10:20 task1_feature_all2.py drwxrwxr-x 2 shifeng shifeng 4096 68 11:36 test -rw-rw-r-- 1 shifeng shifeng 3649 619 10:21 test.py -rw-rw-r-- 1 shifeng shifeng 255 611 21:21 test.py~ -rw-rw-r-- 1 shifeng shifeng 19 625 19:49 test.sh -rw-rw-r-- 1 shifeng shifeng 0 625 19:49 test.sh~ -rw-rw-r-- 1 shifeng shifeng 0 48 22:15 ui_without_buy~ Out[12]: 0 In [13]: from subprocess import Popen In [14]: Popen(["ls","-l"]) Out[14]: <subprocess.Popen at 0xb67c91ac> In [15]: 總用量 40 -rw-rw-r-- 1 shifeng shifeng 3266 53 14:14 all_roc_plot.py~ drwxrwxr-x 6 shifeng shifeng 4096 618 16:59 scrapy_work -rw-rw-r-- 1 shifeng shifeng 12786 610 10:20 task1_feature_all2.py drwxrwxr-x 2 shifeng shifeng 4096 68 11:36 test -rw-rw-r-- 1 shifeng shifeng 3649 619 10:21 test.py -rw-rw-r-- 1 shifeng shifeng 255 611 21:21 test.py~ -rw-rw-r-- 1 shifeng shifeng 19 625 19:49 test.sh -rw-rw-r-- 1 shifeng shifeng 0 625 19:49 test.sh~ -rw-rw-r-- 1 shifeng shifeng 0 48 22:15 ui_without_buy~

1、subprocess.call(command, shell=True)#會直接打印出結果。

2、subprocess.Popen(command, shell=True) 也能夠是subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) 這樣就能夠輸出結果了。假設command不是一個可運行文件,shell=True是不可省略的。shell=True意思是shell下運行command。

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會給你一個像文件的對象從而你能夠使用它來訪問哪個程序的標準輸入、輸出。

并且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)

總結

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

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