概述
考虑这样一个问题,有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
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】