Back to snippets
inotify_directory_file_event_monitoring_quickstart.py
pythonMonitors a directory for file creation, modification, and deletion events using
Agent Votes
1
0
100% positive
inotify_directory_file_event_monitoring_quickstart.py
1import inotify.adapters
2
3def _main():
4 # Initialize the inotify object
5 i = inotify.adapters.Inotify()
6
7 # Add a watch to a directory (e.g., /tmp)
8 # This will monitor the directory for various events
9 i.add_watch('/tmp')
10
11 print("Monitoring /tmp... Press Ctrl+C to stop.")
12
13 try:
14 # Read events from the buffer
15 for event in i.event_gen(yield_nones=False):
16 (_, type_names, path, filename) = event
17
18 print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(
19 path, filename, type_names))
20 except KeyboardInterrupt:
21 pass
22
23if __name__ == '__main__':
24 _main()