最新公告
  • 欢迎您光临网站无忧模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Python与Shell脚本的交互

    正文概述    2020-04-19   286

    考虑这样一个问题,有hello.py脚本,输出”hello, world!”;有TestInput.py脚本,等待用户输入,然后打印用户输入的数据。那么,怎么样把hello.py输出内容发送给TestInput.py,最后TestInput.py打印接收到的”hello, world!”。下面来逐步讲解一下shell的交互方式。

    Python与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!
    0
    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()

    下载网 » Python与Shell脚本的交互

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元