激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

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

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

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

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

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

概述

考慮這樣一個問題,有hello.py腳本,輸出”hello, world!”;有TestInput.py腳本,等待用戶輸入,然后打印用戶輸入的數據。那么,怎么樣把hello.py輸出內容發送給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)

 

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

復制代碼 代碼如下:

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


輸出:

復制代碼 代碼如下:

hello, world!
retcode is: 0


2.os.popen(cmd)

 

執行命令并返回該執行命令程序的輸入流或輸出流.該命令只能操作單向流,與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執行命令,而不是用默認的os.execvp()執行.

復制代碼 代碼如下:

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

 

輸出:

復制代碼 代碼如下:

hello, world!

 

subprocess.Popen()

利用Popen可以是實現雙向流的通信,可以將一個程序的輸出流發送到另外一個程序的輸入流.
Popen()是Popen類的構造函數,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()

 

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 成品片a免人视频 | 久久久久久久久久网 | 久久草在线视频 | 成人在线激情视频 | 亚洲婷婷日日综合婷婷噜噜噜 | 日韩精品dvd | 日本在线播放一区二区 | 亚洲成人精品视频 | 久久亚洲第一 | 久草成人在线 | 91短视频在线播放 | www.精品一区 | 久久av热 | 亚洲成人网一区 | 午夜精品一区二区三区免费 | 羞羞视频免费观看入口 | 激情午夜天 | 91成人午夜性a一级毛片 | 黄色成人小视频 | 久久美女色视频 | 亚洲网站在线播放 | 国内精品久久久久久久久久久久 | 精品中文字幕在线播放 | 成人情欲视频在线看免费 | 999精品国产 | 草草在线观看 | 欧美一级毛片欧美一级成人毛片 | 毛片免费视频网站 | 亚洲午夜影院在线观看 | 黄色网欧美 | 久久99久久99精品 | 三人弄娇妻高潮3p视频 | 久久精品视频3 | 久久久久久久午夜 | 日本在线不卡一区二区 | 久久99国产精品久久99 | 男男成人高潮片免费视频欧美 | 好吊一区二区三区 | 久久亚洲网 | 精品国产一区二区亚洲人成毛片 | 色视频在线播放 |