• 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端口扫描简单程序
  • 用python写的一个wordpress的采集程序
  • win与linux系统中python requests 安装
  • Python线程的两种编程方式
  • Flask的图形化管理界面搭建框架Flask-Admin的使用教程
  • python根据日期返回星期几的方法
  • 在Django的视图(View)外使用Session的方法
  • python实现多线程采集的2个代码例子
  • 详解Python中映射类型的内建函数和工厂函数
  • python实现的文件夹清理程序分享

文章分类

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

最近更新的内容

    • Python虚拟环境Virtualenv使用教程
    • python检测lvs real server状态
    • 在Django中限制已登录用户的访问的方法
    • 在Python的Flask框架中实现单元测试的教程
    • 学习python处理python编码问题
    • 解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
    • Python检测生僻字的实现方法
    • python使用Tkinter显示网络图片的方法
    • 自己编程中遇到的Python错误和解决方法汇总整理
    • 从零学python系列之数据处理编程实例(一)

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

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