Python與shell的3種交互方式介紹
概述
考慮這樣一個問題,有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命令,返回一個返回碼(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可以是實現(xiàn)雙向流的通信,可以將一個程序的輸出流發(fā)送到另外一個程序的輸入流.
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文件實例
- Nodejs中調(diào)用系統(tǒng)命令、Shell腳本和Python腳本的方法和實例
- python中執(zhí)行shell命令的幾個方法小結(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)文章
Python機(jī)器學(xué)習(xí)應(yīng)用之工業(yè)蒸汽數(shù)據(jù)分析篇詳解
本篇文章介紹了如何用Python進(jìn)行工業(yè)蒸汽數(shù)據(jù)分析的過程及思路,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下2022-01-01
AI生成圖片Stable?Diffusion環(huán)境搭建與運(yùn)行方法
Stable?Diffusion是一種基于擴(kuò)散過程的生成模型,由Ge?et?al.在2021年提出,該模型利用了隨機(jī)變量的穩(wěn)定分布,通過遞歸地應(yīng)用擴(kuò)散過程來生成高質(zhì)量的圖像,這篇文章主要介紹了AI圖片生成Stable?Diffusion環(huán)境搭建與運(yùn)行,需要的朋友可以參考下2023-05-05
python高效過濾出文件夾下指定文件名結(jié)尾的文件實例
今天小編就為大家分享一篇python高效過濾出文件夾下指定文件名結(jié)尾的文件實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python循環(huán)語句之break與continue的用法
這篇文章主要介紹了Python循環(huán)語句之break與continue的用法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-10-10

