Python與shell的3種交互方式介紹
概述
考慮這樣一個(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!
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()
- Python中調(diào)用PowerShell、遠(yuǎn)程執(zhí)行bat文件實(shí)例
- Nodejs中調(diào)用系統(tǒng)命令、Shell腳本和Python腳本的方法和實(shí)例
- python中執(zhí)行shell命令的幾個(gè)方法小結(jié)
- shell腳本中執(zhí)行python腳本并接收其返回值的例子
- python調(diào)用shell的方法
- python和shell變量互相傳遞的幾種方法
- python中執(zhí)行shell的兩種方法總結(jié)
- 舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法
- Python下調(diào)用Linux的Shell命令的方法
- 詳解python執(zhí)行shell腳本創(chuàng)建用戶及相關(guān)操作
相關(guān)文章
Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法
這篇文章主要介紹了Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法
這篇文章主要介紹了對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法,注意二者間的區(qū)別,__getattr__只作用于不存在的屬性,需要的朋友可以參考下2016-06-06深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解
這篇文章主要為大家介紹了深度學(xué)習(xí)Tensorflow?2.8?實(shí)現(xiàn)?GRU?文本生成任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01詳解Python調(diào)用系統(tǒng)命令的六種方法
這篇文章主要介紹了詳解Python調(diào)用系統(tǒng)命令的六種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)
這篇文章主要介紹了python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03使用Pytest.main()運(yùn)行時(shí)參數(shù)不生效問題解決
本文主要介紹了使用Pytest.main()運(yùn)行時(shí)參數(shù)不生效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02使用Python的toolz庫開始函數(shù)式編程的方法
這篇文章主要介紹了使用Python的toolz庫開始函數(shù)式編程的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11在Mac OS上使用mod_wsgi連接Python與Apache服務(wù)器
這篇文章主要介紹了在Mac OS上使用mod_wsgi連接Python與Apache服務(wù)器的方法,同時(shí)文中還介紹了使用Python的Django框架時(shí)mod_wsgi連接方式下可能遇到的問題的一般解決方法,需要的朋友可以參考下2015-12-12