0

I am trying to build a python program that follows a log file checks for certain patterns. (Much like grep ..)

Part of the testing code 'test.py' is to read the stdin,

import fileinput
for line in fileinput.input():
    print line

so if I do this in one terminal

tail -f log.txt | python test.py

In another terminal

echo "hello" >> log.txt

you expect hello is print out on the first terminal, but it doesn't. How to change the code? I also want to use it like this

cat log.txt | python test.py

with the same test.py.

Tim
  • 41,901
  • 18
  • 127
  • 145
user40129
  • 763
  • 1
  • 5
  • 15
  • 1
    [This](http://stackoverflow.com/questions/12523044/how-can-i-tail-a-log-file-in-python) might help. – devnull May 06 '14 at 04:21
  • Thanks, but the post you point to seems only about tail -f, I actually want something a bit more general. – user40129 May 06 '14 at 04:32
  • 1
    In the case of `tail -f log.txt | python test.py`, you do not see the output right away because of buffering. In the case of cat, the current contents of the file will get piped to the script. Script will exits once it see an end of file. So, if you write to the file afterwards, you script has already exited. – imran May 06 '14 at 04:40
  • Did you try with `python -u test.py`? – tripleee May 06 '14 at 11:31
  • add -u flag doesn't work either. – user40129 May 06 '14 at 15:38

1 Answers1

0

Echoing sys.stdin directly seems to work on my Mac OS laptop:

import sys
for line in sys.stdin:
    print line.rstrip()

But interestingly, this didn't work very well on my Linux box. It would print the output from tail -f eventually, but the buffering was definitely making it appear as though the program was not working (it would print out fairly large chunks after several seconds of waiting).

Instead I got more responsive behavior by reading from sys.stdin one byte at a time:

import sys

buf = ''
while True:
    buf += sys.stdin.read(1)
    if buf.endswith('\n'):
        print buf[:-1]
        buf = ''
lmjohns3
  • 7,422
  • 5
  • 36
  • 56
  • Interesting! I was on a mac when it was working, but now I'm on an Ubuntu machine and it's not working (either with or without the `-u` flag). – lmjohns3 May 06 '14 at 21:25