概述

考虑这样一个问题,有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()

python里的shell是什么_Python与shell的3种交互方式介绍相关推荐

  1. python echo和linux交互_Python与shell的3种交互方式介绍

    概述 考虑这样一个问题,有hello.py脚本,输出"hello, world!":有TestInput.py脚本,等待用户输入,然后打印用户输入的数据.那么,怎么样把hello. ...

  2. python里的属性是什么_python属性 python 里的属性是什么意思?

    python 里的属性是什么意思? 刚开始看python教程,不是很明白."属性,属性方法,方法"这些是什么意你说的是对象吧 Python是面向对象的语言,在python中一切皆对 ...

  3. python中装饰器的作用_Python装饰器详解,详细介绍它的应用场景

    装饰器的应用场景附加功能 数据的清理或添加:函数参数类型验证 @require_ints 类似请求前拦截数据格式转换 将函数返回字典改为 JSON/YAML 类似响应后篡改为函数提供额外的数据 moc ...

  4. python的命名空间_python中命名空间的三种方式介绍(附示例)

    本篇文章给大家带来的内容是关于python中命名空间的三种方式介绍(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. python中的命名空间分三种: 内置的命名空间,在启动解 ...

  5. python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...

  6. pythonshell窗口是什么_python与shell的3种交互方式介绍

    [目录] 1.os.system(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本,输出"hello, w ...

  7. python里dir是什么意思_python中dir什么作用

    python中dir的作用是:1.dir函数传入数据类型返回该数据类型的所有内置方法:2.dir函数传入模块名返回该模块的所有属性和方法. dir() 内置函数的作用 python 内置方法有很多,无 ...

  8. python的shell无法输入_python中shell如何逐行输入?

    我们都知道逐条输入信息是比较枯燥的,我们在程序中会设置一些脚本来自动完成.我们可以借用python shell这个工具来完成,其打开的方法想必都已经学会了.主要有两种方法:print和转换脚本语言.前 ...

  9. python shell常用命令_python 之 shell命令执行

    python中有几种常用的执行shell命令的模块 1,os.system() 2, os.popen() 3,pexpect.run() 下面介绍3个模块的差别 1,os.system() 直接执行 ...

最新文章

  1. 安卓中事件绑定的写法
  2. ML:MLOps系列讲解之《MLOps Stack Canvas堆栈画布》解读
  3. CST normalize S-parameter to given impedance
  4. asp点击按钮sql列求和_助你2020晋级互联网大数据阵营(一):轻轻松松学SQL
  5. zabbix监控suse linux,SuSE 系统之部署 Zabbix 监控服务
  6. BZOJ1031: [JSOI2007]字符加密Cipher
  7. Java线程volatile(二)
  8. 为什么学习python、python的安装
  9. 药店药品管理方案,药店药品的盘点方案,假设进行药店药品的高效盘点?药品盘点步骤是?...
  10. MySQL 函数:IF(expr,v1,v2) 判断
  11. xmind转excel脚本(简化版)
  12. 人口流动趋势matlab,中国人口增长模型(论文).doc
  13. wps怎么修改云端服务器的地址,新版wps怎么没有云服务器
  14. java g.drawline_菜鸟求教:关于Graphics g中的g.drawLine问题
  15. MFC粉丝(mfcclub)全自动抢购过程(自动识别输入验证码)
  16. 计算机网络教学常用哪些软件,仿真软件在计算机网络教学中的应用
  17. ubuntu系统怎么看电脑配置
  18. S32K144(13)PDB
  19. 华为如何选拔优秀管理者--任正菲
  20. 钉钉消息会话管理,给钉钉传一个url的连接会话传递多个参数的问题

热门文章

  1. Visual Studio开启SSL的支持
  2. 学习nodejs+express+angular+socket.io ,投票demo
  3. 【Android Developers Training】 8. 定义Action Bar风格
  4. jQuery LigerUI 使用教程表格篇(1)
  5. 使用WebService与Oracle EBS进行集成(下)
  6. 最小,独立,可分发的跨平台Web服务器
  7. Python中append()与extend()的区别
  8. TensorFlow 2.0 快速入门指南 | iBooker·ApacheCN
  9. Kali Linux 网络扫描秘籍 第四章 指纹识别(三)
  10. 客服聊天系统,WeLive客服聊天系统