Skip to content

Instantly share code, notes, and snippets.

@atdt
Created March 22, 2012 20:05
Show Gist options
  • Select an option

  • Save atdt/2163023 to your computer and use it in GitHub Desktop.

Select an option

Save atdt/2163023 to your computer and use it in GitHub Desktop.
tail -f multiple files
# tail multiple files
# based on http://www.dabeaz.com/coroutines/follow.py
import sys
import time
import itertools
def tail(filenames):
# open files
files = {f: open(f, 'rt') for f in filenames}
for f in files.values():
f.seek(0, 2)
for name, buf in itertools.cycle(files.items()):
while True:
line = buf.readline()
if not line:
break
yield name, line
time.sleep(0.1)
if __name__ == '__main__':
try:
for filename, line in tail(sys.argv[1:]):
print '[%s] %s' % (filename, line),
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment