How do I watch a file for changes?

前端 未结 25 2772
孤街浪徒
孤街浪徒 2020-11-21 07:13

I have a log file being written by another process which I want to watch for changes. Each time a change occurs I\'d like to read the new data in to do some processing on it

25条回答
  •  余生分开走
    2020-11-21 07:48

    Check my answer to a similar question. You could try the same loop in Python. This page suggests:

    import time
    
    while 1:
        where = file.tell()
        line = file.readline()
        if not line:
            time.sleep(1)
            file.seek(where)
        else:
            print line, # already has newline
    

    Also see the question tail() a file with Python.

提交回复
热议问题