Tag Archives: monitor

Monitoring if a file has changed in Python

The reason I recently looked at creating Windows Services in Python was because I’m interested in monitoring log files. So naturally, I’m also interested when these files are changed or updated. Thought I’d capture some of the basics of watching files with Python.
Check if a path (directory of file) exists:

import os
if os.path.exists("yourpath here"):
print "Yep - found it"

Check if a path is a file (if file doesn’t exist, returns ‘False’):

import os
if os.path.isfile("your path to file"):
print "that's a file alright"

How to compare two (2) files? – You could open them and (if they’re text files) read and compare them line by line. But that would be laborious.
Instead, Python offers the filecmp module.

import filecmp.
filecmp.cmp (filea, fileb)
### returns True of False

(see Python docs – https://docs.python.org/2/library/filecmp.html)

Now, if you’re comparing more than two (2) files, or you would like to compare a file with a prior version of itself, you could generate a checksum (or hash, hash sum) of each file. For that, you use the md5 module. (See Pythond ocs – https://docs.python.org/2/library/md5.html?highlight=md5#md5 )

import md5
hash1 = md5.new()
hash1.update("file1")
hash1.digest() # this generates the checksum

hash2 = md5.new()
hash1.update("file2")
hash2.digest() # this generates the checksum

Then you can compare the two (2) check sums.
Finally, there is os.stat, which allows you to take a peek a file attributes, including the date/time the file was last modified. So if you wanted to just poll a directory or file for that information, you could do:

import os, time
moddate = os.stat("filepath")[8] # there are 10 attributes this call returns and you want the next to last

If you need a readable date/time, try:

print time.ctime(moddate)
Advertisements

Leave a comment

Filed under Python