亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

舉例講解Python程序與系統(tǒng)shell交互的方式

系統(tǒng) 1521 0

概述

考慮這樣一個問題,有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!
0


          

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


          


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美精品成人久久网站 | 成人激情开心网 | 国产香蕉偷在线观看视频 | 久久99亚洲综合精品首页 | 91伦理| 九九热在线精品 | 99久久免费费视频在线观看 | 中文字幕在线日韩 | 亚洲色图国产精品 | 久久午夜综合久久 | 国产小呦 | 久操视频免费 | 手机在线看片国产日韩生活片 | 亚洲视频一区在线播放 | 四虎永久免费地址ww417 | 色综合久久综合网欧美综合网 | 亚洲精品色综合色在线观看 | 天天舔| h视频网站在线观看 | 综合在线亚洲 | 日本特级aⅴ一级毛片 | 国产精品伦一区二区三级视频 | 一区二区在线视频 | 手机在线看片不卡中文字幕 | 国产精品一区二区三区免费 | 日韩精品一区二区三区中文字幕 | 亚洲色视频在线播放网站 | 国产成人91高清精品免费 | 免费观看成人碰视频公开 | 国产精品伦理久久久久 | 国产成人精品一区二区仙踪林 | 在线看福利视频120秒 | 久青草视频在线观看 | 亚洲国产成人在线 | 国产日| 欧美日韩性大片 | 九九视频这里只有精品99 | 一级特级毛片 | 国产欧美在线视频免费 | 久久久精品久久视频只有精品 | 校园春色男人天堂 |