Created
March 22, 2012 20:05
-
-
Save atdt/2163023 to your computer and use it in GitHub Desktop.
tail -f multiple files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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