Digital Geography

19. May 2012

file monitoring using python

In the context of “where are your customers” one main element is to monitor file changes: everytime your customer-file changes the new customer entry must be mapped to a certain point in your webmap. This would enable us to update our density maps or other analytical overview maps in terms of business reporting.

So how to monitor the state of change of a file using python and build in functionalities? The magic module is of course the os module which you need to call:

import os

Now you just need to specify your file you would like to monitor and open the file:

filename = '/adresses.csv'
file = open(filename)

How to get the time of last changes?:

props = os.stat(file)
this = last = props.st_mtime

But you need to compare two different states of times so you need to create a classical while-loop that is always executed as long as the program (we will compile the whole thing in the end…) is executed:

def file_monitor():
while 1:
	if this > last:
		last = this
		##place your function here
		this = props.st_mtime


We will cover the main function in another article next week. Stay tuned.