舉例講解Python程序與系統(tǒng)shell交互的方式
概述
考慮這樣一個(gè)問題,有hello.py腳本,輸出”hello, world!”;有TestInput.py腳本,等待用戶輸入,然后打印用戶輸入的數(shù)據(jù)。那么,怎么樣把hello.py輸出內(nèi)容發(fā)送給TestInput.py,最后TestInput.py打印接收到的”hello, world!”。下面我來逐步講解一下shell的交互方式。
hello.py代碼如下:
#!/usr/bin/python print "hello, world!"
TestInput.py代碼如下:
#!/usr/bin/python str = raw_input() print("input string is: %s" % str)
1.os.system(cmd)
這種方式只是執(zhí)行shell命令,返回一個(gè)返回碼(0表示執(zhí)行成功,否則表示失敗)
retcode = os.system("python hello.py") print("retcode is: %s" % retcode);
輸出:
hello, world! retcode is: 0
2.os.popen(cmd)
執(zhí)行命令并返回該執(zhí)行命令程序的輸入流或輸出流.該命令只能操作單向流,與shell命令單向交互,不能雙向交互.
返回程序輸出流,用fouput變量連接到輸出流
fouput = os.popen("python hello.py") result = fouput.readlines() print("result is: %s" % result);
輸出:
result is: ['hello, world!\n']
返回輸入流,用finput變量連接到輸出流
finput = os.popen("python TestInput.py", "w") finput.write("how are you\n")
輸出:
input string is: how are you
3.利用subprocess模塊
subprocess.call()
類似os.system(),注意這里的”shell=True”表示用shell執(zhí)行命令,而不是用默認(rèn)的os.execvp()執(zhí)行.
f = call("python hello.py", shell=True) print f
輸出:
hello, world! 0
subprocess.Popen()
利用Popen可以是實(shí)現(xiàn)雙向流的通信,可以將一個(gè)程序的輸出流發(fā)送到另外一個(gè)程序的輸入流.
Popen()是Popen類的構(gòu)造函數(shù),communicate()返回元組(stdoutdata, stderrdata).
p1 = Popen("python hello.py", stdin = None, stdout = PIPE, shell=True) p2 = Popen("python TestInput.py", stdin = p1.stdout, stdout = PIPE, shell=True) print p2.communicate()[0] #other way #print p2.stdout.readlines()
輸出:
input string is: hello, world!
整合代碼如下:
#!/usr/bin/python import os from subprocess import Popen, PIPE, call retcode = os.system("python hello.py") print("retcode is: %s" % retcode); fouput = os.popen("python hello.py") result = fouput.readlines() print("result is: %s" % result); finput = os.popen("python TestInput.py", "w") finput.write("how are you\n") f = call("python hello.py", shell=True) print f p1 = Popen("python hello.py", stdin = None, stdout = PIPE, shell=True) p2 = Popen("python TestInput.py", stdin = p1.stdout, stdout = PIPE, shell=True) print p2.communicate()[0] #other way #print p2.stdout.readlines()
- Shell、Perl、Python、PHP訪問 MySQL 數(shù)據(jù)庫代碼實(shí)例
- Python與shell的3種交互方式介紹
- python文件讀寫操作與linux shell變量命令交互執(zhí)行的方法
- Python中調(diào)用PowerShell、遠(yuǎn)程執(zhí)行bat文件實(shí)例
- python執(zhí)行shell獲取硬件參數(shù)寫入mysql的方法
- python中執(zhí)行shell命令的幾個(gè)方法小結(jié)
- shell腳本中執(zhí)行python腳本并接收其返回值的例子
- python調(diào)用shell的方法
- python和shell變量互相傳遞的幾種方法
- PHP webshell檢查工具 python實(shí)現(xiàn)代碼
- Python封裝shell命令實(shí)例分析
相關(guān)文章
pandas數(shù)據(jù)合并之pd.concat()用法詳解
本文主要介紹了pandas數(shù)據(jù)合并之pd.concat()用法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06使用matplotlib實(shí)現(xiàn)在同一個(gè)窗口繪制多個(gè)圖形
這篇文章主要介紹了使用matplotlib實(shí)現(xiàn)在同一個(gè)窗口繪制多個(gè)圖形問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Python實(shí)現(xiàn)的Google IP 可用性檢測(cè)腳本
這篇文章主要介紹了Python實(shí)現(xiàn)的Google IP 可用性檢測(cè)腳本,本文腳本需要Python 3.4+環(huán)境,需要的朋友可以參考下2015-04-04python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程
這篇文章主要為大家介紹了python密碼學(xué)換位密碼及換位解密轉(zhuǎn)置加密教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python中數(shù)字以及算數(shù)運(yùn)算符的相關(guān)使用
這篇文章主要介紹了Python中數(shù)字以及算數(shù)運(yùn)算符的相關(guān)使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10Python 腳本獲取ES 存儲(chǔ)容量的實(shí)例
今天小編就為大家分享一篇Python 腳本獲取ES 存儲(chǔ)容量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12Tensorflow與RNN、雙向LSTM等的踩坑記錄及解決
這篇文章主要介紹了Tensorflow與RNN、雙向LSTM等的踩坑記錄及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05