问题
Is there any way to print a watched file modification ? let's take an example: I'm monitoring file.txt, Pyinotify can tell me that it has been modified, but It can't seem to be able to output what changes occured ? Am I wrong ? How can I see what changes has been made to a file that I'm monitoring ?
Big thanks in advance, sorry for my bad english.
回答1:
The inotify mechanism does not embed the deltas in the event, because it should compute it before saving the files and this could affect performance since this information is not usually needed.
You could use an approach like:
Read the data from
file.txt
and store it in a variable (or another file if persistence is needed).Listen for change events using pyinotify or watchdog.
When the event is fired use difflib to check the deltas from the previous snapshot that you stored and the current file data.
来源:https://stackoverflow.com/questions/25350252/any-ways-to-show-file-changes-with-pyinotify-for-example