[email protected]:/home/ubuntuUser$ cat test.txt
This is a test file
used to validate file handling programs
#pyName: test.txt
this is the last line
[email protected]:/home/ubuntuUser$ cat test.txt | grep "#pyName"
#pyName: test.txt
[email protected]:/home/ubuntuUser$ "
#1 >>> a = subprocess.Popen(['cat test.txt'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
#2 >>> o, e = a.communicate(input='grep #pyName')
#3 >>> print o
#3 This is a test file
#3 used to validate file handling programs
#3 #pyName: test.txt
#3 this is the last line
#3
#3 >>>
Q1:在文件打印只匹配行 外殼grep命令,同時通過子打印grep的整個文件。怎麼了?
Q2: 如何通過communications()發送的輸入添加到 初始命令('cat test.txt')? #2
之後,初始命令是否會在「|」之後通過輸入字符串進行通信,和shell命令獲取像cat test.txt | grep #pyName
?
謝謝Ignatious。這解釋了溝通工作的方式。太好了。因此,如果intial命令正在等待stdin輸入,則通過通信發送的命令會將其發送到該命令的stdin。再次感謝! – Prasath 2013-03-26 23:32:02