一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python與shell的3種交互方式介紹

Python與shell的3種交互方式介紹

2020-06-03 10:09腳本之家 Python

這篇文章主要介紹了Python與shell的3種交互方式介紹,本文講解了os.system、os.popen、subprocess模塊等3種方法,需要的朋友可以參考下

概述

考慮這樣一個(gè)問(wèn)題,有hello.py腳本,輸出”hello, world!”;有TestInput.py腳本,等待用戶輸入,然后打印用戶輸入的數(shù)據(jù)。那么,怎么樣把hello.py輸出內(nèi)容發(fā)送給TestInput.py,最后TestInput.py打印接收到的”hello, world!”。下面我來(lái)逐步講解一下shell的交互方式。

hello.py代碼如下:

復(fù)制代碼 代碼如下:

#!/usr/bin/python
print "hello, world!"


TestInput.py代碼如下:

復(fù)制代碼 代碼如下:

#!/usr/bin/python
str = raw_input()
print("input string is: %s" % str)


1.os.system(cmd)

 

這種方式只是執(zhí)行shell命令,返回一個(gè)返回碼(0表示執(zhí)行成功,否則表示失敗)

復(fù)制代碼 代碼如下:

retcode = os.system("python hello.py")
print("retcode is: %s" % retcode);


輸出:

復(fù)制代碼 代碼如下:

hello, world!
retcode is: 0


2.os.popen(cmd)

 

執(zhí)行命令并返回該執(zhí)行命令程序的輸入流或輸出流.該命令只能操作單向流,與shell命令單向交互,不能雙向交互.

返回程序輸出流,用fouput變量連接到輸出流

復(fù)制代碼 代碼如下:

fouput = os.popen("python hello.py")
result = fouput.readlines()
print("result is: %s" % result);

 

輸出:

復(fù)制代碼 代碼如下:

result is: ['hello, world!\n']

 

返回輸入流,用finput變量連接到輸出流

復(fù)制代碼 代碼如下:

finput = os.popen("python TestInput.py", "w")
finput.write("how are you\n")


輸出:

復(fù)制代碼 代碼如下:

input string is: how are you

 

3.利用subprocess模塊

subprocess.call()

類似os.system(),注意這里的”shell=True”表示用shell執(zhí)行命令,而不是用默認(rèn)的os.execvp()執(zhí)行.

復(fù)制代碼 代碼如下:

f = call("python hello.py", shell=True)
print f

 

輸出:

復(fù)制代碼 代碼如下:

hello, world!

 

subprocess.Popen()

利用Popen可以是實(shí)現(xiàn)雙向流的通信,可以將一個(gè)程序的輸出流發(fā)送到另外一個(gè)程序的輸入流.
Popen()是Popen類的構(gòu)造函數(shù),communicate()返回元組(stdoutdata, stderrdata).

復(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()

 

輸出:

復(fù)制代碼 代碼如下:

input string is: hello, world!

 

整合代碼如下:

復(fù)制代碼 代碼如下:


#!/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()

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 密臀tv | 亚洲精品精品一区 | 天天色一色 | 国产成人精品日本亚洲网址 | 久久亚洲伊人 | 成年人视频在线播放 | 香港日本三级亚洲三级 | 欧美日韩精品一区二区三区视频在线 | 久久理论片 | 美女扒开胸罩露出奶了无遮挡免费 | 色中文 | 狠狠撸在线播放 | a级在线看 | 手机能看的黄色网站 | 99热综合在线 | 国产福利在线观看91精品 | 亚洲天堂视频在线观看免费 | 牛牛影院成人免费网页 | 日本肉体xxxx | 乖女的嫩奶水h文孕妇 | 大又大又粗又爽女人毛片 | 99草| 精选国产AV精选一区二区三区 | 99九九国产精品免费视频 | 日韩有码 | 色综合久久中文字幕综合网 | 动漫美女被吸乳羞羞小说 | 美艳教师刘艳第三部166 | 久热这里只有精品99国产6 | 亚州成人 | 亚洲精品成人在线 | 亚洲精品一 | 扒开放荡老师裙子猛烈的进入 | 亚洲图片一区二区 | 调教催眠改造np总攻 | 男女男精品网站免费观看 | 糖心vlog麻豆精东影业传媒 | 操破苍穹全文阅读 | 国内精品视频一区二区三区八戒 | 动漫美女隐私尿口图片 | 扒开双腿羞辱调教play视频 |