• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 举例讲解Python程序与系统shell交互的方式

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

作者:goldensun 字体:[增加 减小] 来源:互联网

goldensun 通过本文主要向大家介绍了举例说明shell的功能,举例说明程序性知识,程序性知识举例,举例说明什么是程序,c语言程序举例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

概述

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

hello.py代码如下:

#!/usr/bin/python
print "hello, world!"

</div>


TestInput.py代码如下:

#!/usr/bin/python
str = raw_input()
print("input string is: %s" % str)

</div>

1.os.system(cmd)

这种方式只是执行shell命令,返回一个返回码(0表示执行成功,否则表示失败)

retcode = os.system("python hello.py")
print("retcode is: %s" % retcode);

</div>

输出:

hello, world!
retcode is: 0

</div>

2.os.popen(cmd)

执行命令并返回该执行命令程序的输入流或输出流.该命令只能操作单向流,与shell命令单向交互,不能双向交互.
返回程序输出流,用fouput变量连接到输出流

fouput = os.popen("python hello.py")
result = fouput.readlines()
print("result is: %s" % result);

</div>

输出:

result is: ['hello, world!\n']

</div>

返回输入流,用finput变量连接到输出流

finput = os.popen("python TestInput.py", "w")
finput.write("how are you\n")

</div>

输出:

input string is: how are you

</div>

3.利用subprocess模块

subprocess.call()
</div>

类似os.system(),注意这里的”shell=True”表示用shell执行命令,而不是用默认的os.execvp()执行.

f = call("python hello.py", shell=True)
print f

</div>

输出:

hello, world!
0

</div>

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()

</div>

输出:

input string is: hello, world!

</div>

整合代码如下:

#!/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()

</div>

</div>

您可能想查找下面的文章:

  • 举例讲解Linux系统下Python调用系统Shell的方法
  • 举例讲解Python程序与系统shell交互的方式
  • 举例讲解Python程序与系统shell交互的方式

相关文章

  • Python 执行字符串表达式函数(eval exec execfile)
  • 查看Python安装路径以及安装包路径小技巧
  • python实现文本去重且不打乱原本顺序
  • 完美解决python遍历删除字典里值为空的元素报错问题
  • 50行代码实现贪吃蛇(具体思路及代码)
  • 使用FastCGI部署Python的Django应用的教程
  • Windows和Linux下使用Python访问SqlServer的方法介绍
  • Python实现截屏的函数
  • python比较两个列表大小的方法
  • Python 爬虫学习笔记之多线程爬虫

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python常用小技巧总结
    • 从零学Python之入门(五)缩进和选择
    • Python自动化构建工具scons使用入门笔记
    • Python 实现链表实例代码
    • python读取Android permission文件
    • Python urls.py的三种配置写法实例详解
    • 跟老齐学Python之玩转字符串(1)
    • Python随机生成彩票号码的方法
    • python读取TXT到数组及列表去重后按原来顺序排序的方法
    • python下os模块强大的重命名方法renames详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有