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:
Check if a path is a file (if file doesn’t exist, returns ‘False’):
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.
(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 )
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:
If you need a readable date/time, try: