- Monitoring if a file has changed in Python
- How to Detect File Changes with Python
- Start your Python IDE
- Create a new Python File called FileModificationHandler.py
- Detect File Changes
- Detecting File Changes
- update-check 0.0.11
- Usage
- Checking if File is Up to Date
- Syntax:
- Updating a File
- Syntax:
- Updating a File if it’s not up to date
- Syntax:
- Monitoring if a file has changed in Python
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
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:
How to Detect File Changes with Python
In this article we will see how to detect file changes using Python.
There are plenty of daily programming scenarios where we need to monitor file changes. Recently I was in a situation which called for a way to detect file updates. WatchDog was an obvious solution to my problem. But because it fired multiple events on a single update I decided to write my own code.
In this article we will write a class called FileModificationHandler. This class will allow us to execute a callback when someone changes the file. So let’s start the coding process.
Start your Python IDE
First start your favorite IDE. For me that would be PyCharm.
Create a new Python File called FileModificationHandler.py
This class will host all of the code related to file change detection process. So let’s start by importing the libraries we are going to be using:
import os import time import traceback
Then we declare the class with it’s constructor like so:
class FileModified(): def __init__(self, file_path, callback): self.file_path = file_path self.callback = callback self.modifiedOn = os.path.getmtime(file_path)
The class has two mandatory parameters. A file path pointing to the file we will be monitoring, and a callback. A callback is simply a function. In Python, functions are just more objects, and so we can pass them around as arguments.
This function is provided by the consumer of the class. A file change will invoke this callback. Thus, notifying the consumer about the change. This way the consumer may process the event accordingly.
Detect File Changes
In this next section we will implement the file monitoring code.
def start(self): try: while (True): time.sleep(0.5) modified = os.path.getmtime(self.file_path) if modified != self.modifiedOn: self.modifiedOn = modified if self.callback(): break except Exception as e: print(traceback.format_exc())
First we will create an infinite while loop. As long as the script is running, it will check for file updates. The OS module in Python provides functions for interacting with the operating system. The OS module contains a sub module called path. What we are looking for is the getmtime method. This method is used to get the last time of last modification of the specified path. It returns a floating point value. This value represents the number of seconds since the epoch.
As you can imagine, if this value is different on the next check then we can comfortably fire the callback, because the file has changed. I must mention that the callback in my example is actually a predicate. A predicate is nothing more then a function returning True or False. This way I allow the consumer to control the flow of the process. If the predicate returns a True value then we are breaking out of the infinite while cycle. In other words, we stop the file monitoring process.
This may be valuable when we want to detect the first three changes to a file, for example. But, as long as the predicate returns False the file monitoring process will continue.
There is one more caveat I want to mention. As you can see we do suspend the running thread by using time.sleep(0.5) for a half a second. This statement allows other threads to receive control over the CPU.
Detecting File Changes
To detect file changes we must first instantiate an object of our FileModifiedHandler class. But, before we do that we must import that class
from FileModificationHandler import FileModified
Then we need to define our callback function
def file_modified(): print("File Modified!") return False
Remember the callback is a predicate which means that it needs to return True or False. If for some reason you would like to stop monitoring the file, just return True.
For the purposes of this demo I will simply write “File Modified!” to the console and continue the file monitoring process until the script is terminated.
Now let’s create an instance of out FileModified class like so:
fileModifiedHandler = FileModified(r"test file.txt",file_modified)
Where “test file.txt” is just a text file I have placed inside my script folder. We will detect file changes on that particular path.
Finally, let’s start the process by invoking the start method
This is the code listing in my PyCharm IDE:
So every time I make a change on the “test file.txt” I get notified that the file is updated and a appropriate message is printed in the output window.
update-check 0.0.11
Update_Check is a Python module to implement in distributed programs. The functions as of now are checking for updates and updating a users Python file. It works by comparing the users Python file to the newest raw version of the file that’s stored on a website like GitHub.
Usage
Checking if File is Up to Date
One feature of this module is checking if the users file is up to date with the latest version.
Syntax:
pathToProgram(str): path to local file to be compared
Heres an example of the isUpToDate() function:
This function will return True if the program is up to date with the web version and will return False if it's not. Note: __file__ can be replaced with a path to a separate file.
Updating a File
To update a file using this module, you will need to use the update() function.
Syntax:
Here's an example of the update() function:
That block of code would grab the file from the URL and update the local file to that file. Note: __file__ can be replaced with a path to a separate file.
Updating a File if it’s not up to date
If you want a combination of the previous 2 functions, where it will check if a file isn’t up to date, and if so, then it will update the file, then you want to use the checkForUpdates() function.
Syntax:
Here's an example of the checkForUpdates() function:
The above block of code will test if the file specified, is not up to date, and if it isn't, it will update the file from the URL and return True. If it is up to date, then it will return False and do nothing. Note: __file__ can be replaced with a path to a separate file.
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
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: