Tuesday, November 28, 2017

Checking a Directory for Changes with Python

This has been something I've been meaning to write a post about for a while, and I finally found myself with a free moment to document the process!

I recently needed to develop a python script that I could utilize for checking a specific file directory for changes.  By "changes", I mean any additions, deletions, or modifications to the files located within a specific directory.  For this exercise, I was not concerned with the type of change that was occurring, but only the time/date the change occurred.

Below is the script I came up with for this process, which reports on the 'Date Modified' column for a specific directory:

                                                                                                                               
#Quick and dirty method for checking last modified date
#for a folder or file path
#Tim Hohn 11/15/2017

#import modules
import os.path, time

#print date modified for specified file directory
print("Export_Test Folder - Last modified: %s" % time.ctime(os.path.getmtime("H:\Desktop\Test\Export_Test")))
                                                                                                                                

Running this script once every morning has been helpful towards keeping up with changes to a specific directory, especially in responding to new documents that have been uploaded that may require immediate attention.  Setting the script to run at a specific time through Task Scheduler, further assists the process and eliminates the need for me to remember to check the directory for changes every day.



Alternatively, in my research for the above process I came across a "watchdog" script that will specify the type of change (added or removed) that occurred.

                                                                                                                                 
#Watchdog designed to alert the user when files have been
#added, removed or updated within a directory
#Original Source Here:  http://timgolden.me.uk/python/
#win32_how_do_i/watch_directory_for_changes.html

#Import Modules
import os, time

#Set Path to Watch
path_to_watch = "H:\Desktop\Test\Export_Test"
before = dict ([(f, None) for f in os.listdir (path_to_watch)])

#Print Current Time
print time.asctime( time.localtime(time.time()) )
      
#Watch for changes to the directory specified in path_to_watch
while 1:
  time.sleep (0.5)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: print "Added: ", ", ".join (added)
  if removed: print "Removed: ", ", ".join (removed)
  before = after
                                                                                                                                  



So there you have it!  A couple of different methods for checking a directory for changes within python.

Ideas for next steps to make these scripts more useful:
          1.  Generate a text report of changes daily
          2.  E-mail report to personal e-mail daily
          3.  Run overnight and have report ready and sent to inbox in A.M.

Work in Progress - Disputed Areas Map

I started this project as a fun change of pace from my normal GIS day-to-day, and as a way of brushing up on my cartography skills. For me,...