一个看起来很像的简单过滤脚本
import sys
for line in sys.stdin:
print line
如果 tail -f
的输出通过管道传输,则不会打印任何内容,但与 cat
的输出一起工作正常。 grep
然而,tail -f
没有问题,所以我想我应该以某种方式改变脚本处理输入的方式。
最佳答案
根据 python(1) 手册页:
Note that there is internal buffering in xreadlines(), readlines() and file-object iterators ("for line insys.stdin") which is not influenced by this option. To work around this, you will want to use "sys.stdin.readline()" inside a "while 1:" loop.
请尝试以下操作:
import sys
while True:
line = sys.stdin.readline()
if not line:
break
sys.stdout.write(line)
sys.stdout.flush()
关于python - 使用Python过滤tail的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18656436/