Python使用管道(pipe)使程序通信
今天在看Python的知识时,发现了Windows下使用“|”,即管道,由于基本上没接触过因此觉得很新奇,还能通过管道配合Python的标准输入输出流来进行不同程序间的通信,传值。“|”之前的输出是“|”之后程序的输入,这点以前都没琢磨过来,只知道是这么用的,更不知道windows也行!!!
写了两个文件分别为write1.py,read1.py
write1.py:
print("Hello World!")
print(50)
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)
import sys
data = sys.stdin.readline()[:-1]
print('this data is :'+data+' double is :',int(data)*2)
调用:
D:\IStudy\Java\workspace\mypy\com\dgb\test>python write1.py | read1.py
this is my got string:Hello World!
this data is :50 double is : 100
this is my got string:Hello World!
this data is :50 double is : 100
stdin
is used for all interactive input (including calls toinput()
);stdout
is used for the output ofprint()
and expression statements and for the prompts ofinput()
;
特意记录下,觉得写一些小程序时非常有用。