举例讲解Python程序与系统shell交互的方式
作者:goldensun 发布时间:2021-10-29 14:43:17
标签:Python,shell
概述
考虑这样一个问题,有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中对信号处理的模块主要是使用signal模块,但signal主要是针对Unix系统,所以在Windows平台上Python不能很
- 在开始之前,先问问大家:什么是百度Aip模块?百度AI平台提供了很多的API接口供开发者快速的调用运用在项目中本文写的是使用百度AI的**在
- mysql 索引详解:在mysql 中,索引可以分为两种类型 hash索引和 btree索引。 什么情况下可以用到B树索引?&nb
- 目录一、简介1、优势2、劣势二、安装三、locust的库和方法介绍1、from locust import task2、from locus
- 一、基本概述目前电脑上已经下载了MongoDB数据库、navicat for mongodb作为mongoDB的可视化工具,形如navica
- 理论知识部分:一、简单总结几点数据库测试点:1.检查接口返回的数据是否与预期一致2.传递数据类型错误时能否处理,比如数据类型要求是整数,传递
- 第一次写这类文章,有点儿紧张有点儿新奇有点儿痛苦,来CDC实习2个月啦,每天除了工作就是体验体验再体验,因为之前做了一些有关规范的工作,突然
- 1、日期大小的比较,传到xml中的日期格式要符合'yyyy-MM-dd',这样才能走索引,如:'yyyy'改
- 先来看看绘制的动态水球图:没有安装PyEcharts的,先安装PyEcharts:# 安装pyecharts模块,直接安装就是最新的版本pi
- pygame创建游戏窗口界面,供大家参考,具体内容如下使用pygame前一定要先导入pygame而且肯定要先初始化pygameimport
- 在win7 64位,Anaconda安装的Python3.6.1下安装的TensorFlow与Keras,Keras的backend为Ten
- 在Python程序中导入ctypes模块,载入动态链接库。动态链接库有三种:cdll以及windows下的windll和oledll,cdl
- 前言本文将教你如何使用YOLOV3对象检测器、OpenCV和Python实现对图像和视频流的检测。用到的文件有yolov3.weights、
- Hello,World,几乎是程序猿学习各种语言的第一个程序,心血来潮,有空拿主流开发语言如何实现,汇总并整理了下.包括大致快速了解下这门语
- 本文教大家用原生js实现的简单网页主页右下角的广告框效果,利用好绝对定位,点击X关闭广告,里面的内容不管动图或者视频都可以。 代码最简洁,j
- 如果你细心跟踪一下SQL Server数据库服务器的登录过程,你会发现口令计算其实是非常脆弱的,SQL Server数据库的口令脆弱体现两方
- 在Oracle 8i版本之前,使用internal用户来执行数据库的启动和关闭以及create database等操作;从8i版本以后,Or
- 原理1.使用python中的mtplotlib库。2.立体爱心面公式点画法(实心)代码import matplotlib.pyplot as
- 本文实例为大家分享了HTML5 JS压缩图片,并获取图片BASE64编码上传的方法,供大家参考,具体内容如下基本过程1) 调用 FileRe
- JS获取多选框checkbox被选中的个数。var checkbox = document.getElementsByName("