举例讲解Python程序与系统shell交互的方式

 更新时间:2015年04月09日 09:50:51   投稿:goldensun  
这篇文章主要介绍了Python程序与系统shell交互的方式,举了一个非常简单的hello world的例子,需要的朋友可以参考下

概述

考虑这样一个问题,有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!
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 用户输入和while循环的操作

    Python 用户输入和while循环的操作

    这篇文章主要介绍了Python 用户输入和while循环的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python实现数字的格式化输出

    Python实现数字的格式化输出

    这篇文章主要介绍了Python如何实现数字的格式化输出,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-08-08
  • 解决Python3 struct报错argument for 's' must be a bytes object

    解决Python3 struct报错argument for 's'&

    这篇文章主要为大家介绍了解决Python3 struct报错argument for 's' must be a bytes object方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • Python3多线程基础知识点

    Python3多线程基础知识点

    在本篇内容里小编给大家分享了关于Python3多线程基础知识点内容,需要的朋友们跟着学习参考下。
    2019-02-02
  • 使用Python实现全摄像头拍照与键盘输入监听功能

    使用Python实现全摄像头拍照与键盘输入监听功能

    这篇文章主要介绍了使用Python实现全摄像头拍照与键盘输入监听功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • 你真的了解Python的random模块吗?

    你真的了解Python的random模块吗?

    这篇文章主要介绍了Python的random模块的相关内容,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • Python实现变量数值交换及判断数组是否含有某个元素的方法

    Python实现变量数值交换及判断数组是否含有某个元素的方法

    这篇文章主要介绍了Python实现变量数值交换及判断数组是否含有某个元素的方法,涉及Python字符串与数组的相关赋值、判断操作技巧,需要的朋友可以参考下
    2017-09-09
  • Python修改游戏内存的方法

    Python修改游戏内存的方法

    本文给大家分享一个通过Python来修改游戏内存的方法,帮助大家更改游戏中的数据,步骤很简单,而且有视频讲解,感兴趣的朋友一起看看吧
    2021-11-11
  • PyCharm更换pip源、模块安装以及PyCharm依赖包导入导出功能

    PyCharm更换pip源、模块安装以及PyCharm依赖包导入导出功能

    这篇文章主要给大家介绍了关于PyCharm更换pip源、模块安装以及PyCharm依赖包导入导出功能的相关资料,我们在使用pycharm的时候,pycharm中的虚拟环境依赖包需要导出成一个文件,需要的朋友可以参考下
    2023-11-11
  • python爬虫工具例举说明

    python爬虫工具例举说明

    在本篇文章里小编给大家整理的是一篇关于python爬虫工具例举说明内容,有兴趣的朋友们可以学习下。
    2020-11-11

最新评论