概述
Python使用管道(pipe)使程序通信
今天在看Python的知识时,发现了Windows下使用“|”,即管道,由于基本上没接触过因此觉得很新奇,还能通过管道配合Python的标准输入输出流来进行不同程序间的通信,传值。“|”之前的输出是“|”之后程序的输入,这点以前都没琢磨过来,只知道是这么用的,更不知道windows也行!!!
写了两个文件分别为write1.py,read1.py
write1.py:
print("Hello World!")
print(50)
read.py
print('this is my got string:%s'%input())
import sys
data = sys.stdin.readline()[:-1]
print('this data is :'+data+' double is :',int(data)*2)
调用:
D:IStudyJavaworkspacemypycomdgbtest>python write1.py | read1.py
this is my got string:Hello World!
this data is :50 double is : 100stdin is used for all interactive input (including calls toinput());
stdout is used for the output of print() and expression statements and for the prompts ofinput();
从文档的说明可以看出第一个input接收了write1的第一个print,第二个sys.stdin接收了write1.py的第二个print。
特意记录下,觉得写一些小程序时非常有用。
最后
以上就是诚心美女为你收集整理的pythonwindows管道_Python使用管道(pipe)协同标准的全部内容,希望文章能够帮你解决pythonwindows管道_Python使用管道(pipe)协同标准所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复