
Otherwise your application will get stuck in an infinite loop and you will not see your game updating.
PYTHON MONITOR NETWORK UPDATE
When used in pygame make sure the stuff in the 'while' loop is placed in your game loop aka update or whatever. I made this when playing with pygame so I can see effects take place immediately after file save. Handy tool for restarting application when changes have been made to the source. One that may not be the best way of doing it, but it sure is a short way. This is an example of checking a file for changes. Print('Watching '.format(', '.join(modified))) Usage: whateverName.py path_to_dir_to_watch #!/usr/bin/env pythonįiles = This is another modification of Tim Goldan's script that runs on unix types and adds a simple watcher for file modification by using a dict (file=>time). Thanks everyone for your input - great stuff! It could probably do with a load more error checking, but for simply watching a log file and doing some processing on it before spitting it out to the screen, this works well. #print file, ACTIONS.get (action, "Unknown") # For each change, check to see if it's updating the file we're interested inįull_filename = os.path.join (path_to_watch, file)

Results = win32file.ReadDirector圜hangesW ( # Wait for new data and call ProcessNewData for each new chunk that's written Win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, Path_to_watch = "." # look at the current directoryįile_to_watch = "test.txt" # look for changes to a file called test.txt Well after a bit of hacking of Tim Golden's script, I have the following which seems to work quite well: import os Watcher = Watcher(watch_file, custom_action, text='yes, changed') # also call custom action function # Call this function each time a change happens Print('Unhandled error: %s' % sys.exc_info())

Self.call_func_on_change(*self.args, **self.kwargs) Self.call_func_on_change = call_func_on_change

For watching a single file with polling, and minimal dependencies, here is a fully fleshed-out example, based on answer from Deestan (above): import osĭef _init_(self, watch_file, call_func_on_change=None, *args, **kwargs):
