举例讲解Python程序与系统shell交互的方式
作者:goldensun 时间:2021-10-29 14:43:17
概述
考虑这样一个问题,有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,shell
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm远程连接服务器并配置python interpreter的方法
2023-03-21 20:36:06
![](https://img.aspxhome.com/file/2023/7/82897_0s.png)
python 实现GUI(图形用户界面)编程详解
2023-10-05 15:11:34
python中lambda()的用法
2022-07-19 05:15:45
Python reques接口测试框架实现代码
2023-10-07 12:47:08
![](https://img.aspxhome.com/file/2023/9/63219_0s.png)
python3 wechatpy微信支付的项目实践
2023-08-29 14:13:36
![](https://img.aspxhome.com/file/2023/5/87975_0s.jpg)
Python3列表内置方法大全及示例代码小结
2022-09-23 01:39:21
![](https://img.aspxhome.com/file/2023/1/72141_0s.jpg)
关于python简单的爬虫操作(requests和etree)
2022-01-08 02:17:27
![](https://img.aspxhome.com/file/2023/8/69568_0s.jpg)
永不熄灭的爱心图标——腾讯公益月捐计划 “QQ首席图标”诞生记
2009-09-01 19:43:00
![](https://img.aspxhome.com/file/UploadPic/20099/1/0909_loveplan_banner-44s.jpg)
PHP与以太坊交互详解
2023-10-21 13:59:36
编写SQL Server的扩展存储过程实例
2009-01-20 15:45:00
python处理cookie详解
2023-09-27 19:14:36
Facebook基础的信息架构图
2008-04-01 09:46:00
![](https://img.aspxhome.com/file/UploadPic/20084/1/20084194941397s.png)
PHP使用laravel邮件服务发送邮件
2023-05-25 03:48:23
OpenCV 图像梯度的实现方法
2023-07-14 08:25:43
![](https://img.aspxhome.com/file/2023/6/64296_0s.png)
使用 Python 玩转 GitHub 的贡献板(推荐)
2021-10-10 01:12:11
![](https://img.aspxhome.com/file/2023/5/104925_0s.png)
一场关于YUI3/jQuery的精彩辩论
2010-11-11 12:50:00
python列表推导式入门学习解析
2022-04-25 14:28:00
正则表达式结合数组提取文章中的文件名
2007-10-12 13:59:00
python之np.argmax()及对axis=0或者1的理解
2021-01-27 19:28:32
![](https://img.aspxhome.com/file/2023/7/67357_0s.png)
python实现csv格式文件转为asc格式文件的方法
2021-10-12 19:10:37
![](https://img.aspxhome.com/file/2023/3/72823_0s.jpg)