Python与shell的3种交互方式介绍

 更新时间:2015年04月11日 11:19:03   投稿:junjie  
这篇文章主要介绍了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()

相关文章

  • 使用Nibabel库对nii格式图像的读写操作

    使用Nibabel库对nii格式图像的读写操作

    这篇文章主要介绍了使用Nibabel库对nii格式图像的读写操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Django多进程滚动日志问题解决方案

    Django多进程滚动日志问题解决方案

    这篇文章主要介绍了Django多进程滚动日志问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Pandas缺失值删除df.dropna()的使用

    Pandas缺失值删除df.dropna()的使用

    本文主要介绍了Pandas缺失值删除df.dropna()的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • selenium在scrapy中的使用代码

    selenium在scrapy中的使用代码

    本文给大家分享selenium在scrapy中的使用代码,使用selenium可以很好的帮助我们获取一些重要数据信息,本文通过代码给大家详细介绍,感兴趣的朋友跟随小编一起看看吧
    2021-05-05
  • Python机器学习应用之工业蒸汽数据分析篇详解

    Python机器学习应用之工业蒸汽数据分析篇详解

    本篇文章介绍了如何用Python进行工业蒸汽数据分析的过程及思路,通读本篇对大家的学习或工作具有一定的价值,需要的朋友可以参考下
    2022-01-01
  • AI生成图片Stable Diffusion环境搭建与运行方法

    AI生成图片Stable Diffusion环境搭建与运行方法

    Stable Diffusion是一种基于扩散过程的生成模型,由Ge et al.在2021年提出,该模型利用了随机变量的稳定分布,通过递归地应用扩散过程来生成高质量的图像,这篇文章主要介绍了AI图片生成Stable Diffusion环境搭建与运行,需要的朋友可以参考下
    2023-05-05
  • python高效过滤出文件夹下指定文件名结尾的文件实例

    python高效过滤出文件夹下指定文件名结尾的文件实例

    今天小编就为大家分享一篇python高效过滤出文件夹下指定文件名结尾的文件实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • Pytorch入门之mnist分类实例

    Pytorch入门之mnist分类实例

    这篇文章主要为大家详细介绍了Pytorch入门之mnist分类实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • Python循环语句之break与continue的用法

    Python循环语句之break与continue的用法

    这篇文章主要介绍了Python循环语句之break与continue的用法,是Python入门学习中的基础知识,需要的朋友可以参考下
    2015-10-10
  • Python发送邮件封装实现过程详解

    Python发送邮件封装实现过程详解

    这篇文章主要介绍了Python发送邮件封装实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05

最新评论