parsing - Watch a directory in python and parse latest generated text file -


i'd ask if there's way watch directory in python , parse latest text file being generated on directory.

tho have start code parse text file.

import time  def follow(thefile):     thefile.seek(0,2)     while true:         line = thefile.readline()         if not line:             time.sleep(0.1)             continue         yield line   if __name__ == '__main__':     logfile = open(r'\\some directory\files.txt',"r")     loglines = follow(logfile)     line in loglines:         print line, 

see bold files.txt need dynamic watching directory newly generated text files , switch latest text file , parse it.

it run on windows xp service pack 3

i'm using python 2.7

directory i'm watching using windows xp

thank you.

to check new files, repeatedly list of files in directory os.listdir('directory'). save entries in set , calculate difference of set previous set.

# initialize before event loop: old_entries = set()  # need loop calls 2 handlers, each handler returning soon. # inside loop, check "new file" event way: now_entries = os.listdir(r'\\some directory') now_entries.symmetric_difference_update(old_entries) new_entry in now_entries:     handle_new_file(new_entry) 

your program needs listen 2 events:

  • new file in directory.
  • new line in old file.

you call follow(), event handler never returns. think want handler return 1 main event loop checks each kind of event. follow() function never returns because continues within while true infinite loop unless new line added file yield. never yield if no more lines getting added file.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -