午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    Python與shell的3種交互方式介紹
    來源:易賢網(wǎng) 閱讀:2495 次 日期:2015-04-14 15:24:03
    溫馨提示:易賢網(wǎng)小編為您整理了“Python與shell的3種交互方式介紹”,方便廣大網(wǎng)友查閱!

    概述

    考慮這樣一個(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()

    更多信息請(qǐng)查看IT技術(shù)專欄

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:Python與shell的3種交互方式介紹
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)