How do I watch a file for changes?

前端 未结 25 3257
孤街浪徒
孤街浪徒 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 08:05

    Here is a simplified version of Kender's code that appears to do the same trick and does not import the entire file:

    # Check file for new data.
    
    import time
    
    f = open(r'c:\temp\test.txt', 'r')
    
    while True:
    
        line = f.readline()
        if not line:
            time.sleep(1)
            print 'Nothing New'
        else:
            print 'Call Function: ', line
    

提交回复
热议问题