欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年04月11日 11:19:03   投稿:junjie  
這篇文章主要介紹了Python與shell的3種交互方式介紹,本文講解了os.system、os.popen、subprocess模塊等3種方法,需要的朋友可以參考下

概述

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

相關(guān)文章

  • Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法

    Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法

    這篇文章主要介紹了Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法

    對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法

    這篇文章主要介紹了對(duì)比Python中__getattr__和 __getattribute__獲取屬性的用法,注意二者間的區(qū)別,__getattr__只作用于不存在的屬性,需要的朋友可以參考下
    2016-06-06
  • 深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解

    深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解

    這篇文章主要為大家介紹了深度學(xué)習(xí)Tensorflow?2.8?實(shí)現(xiàn)?GRU?文本生成任務(wù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • python中利用xml.dom模塊解析xml的方法教程

    python中利用xml.dom模塊解析xml的方法教程

    這篇文章主要給大家介紹了關(guān)于python中利用xml.dom模塊解析xml的方法教程,文中通過示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • 詳解Python調(diào)用系統(tǒng)命令的六種方法

    詳解Python調(diào)用系統(tǒng)命令的六種方法

    這篇文章主要介紹了詳解Python調(diào)用系統(tǒng)命令的六種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)

    python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)

    這篇文章主要介紹了python openpyxl 帶格式復(fù)制表格的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 使用Pytest.main()運(yùn)行時(shí)參數(shù)不生效問題解決

    使用Pytest.main()運(yùn)行時(shí)參數(shù)不生效問題解決

    本文主要介紹了使用Pytest.main()運(yùn)行時(shí)參數(shù)不生效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 使用Python的toolz庫開始函數(shù)式編程的方法

    使用Python的toolz庫開始函數(shù)式編程的方法

    這篇文章主要介紹了使用Python的toolz庫開始函數(shù)式編程的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 使用numpy和PIL進(jìn)行簡單的圖像處理方法

    使用numpy和PIL進(jìn)行簡單的圖像處理方法

    今天小編就為大家分享一篇使用numpy和PIL進(jìn)行簡單的圖像處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 在Mac OS上使用mod_wsgi連接Python與Apache服務(wù)器

    在Mac OS上使用mod_wsgi連接Python與Apache服務(wù)器

    這篇文章主要介紹了在Mac OS上使用mod_wsgi連接Python與Apache服務(wù)器的方法,同時(shí)文中還介紹了使用Python的Django框架時(shí)mod_wsgi連接方式下可能遇到的問題的一般解決方法,需要的朋友可以參考下
    2015-12-12

最新評(píng)論