
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to monitor Python files for changes?
Monitoring files for changes in any language is hard because of cross platform issues. On python, there is a widely used cross platform library called watchdog that allows watching for changes. You can install it using:
$ pip install watchdog
Example
To watch a file called 'my_file.txt' using watchdog, you can use the simple program:
import time from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class MyHandler(FileSystemEventHandler): def on_modified(self, event): print("Got it!") event_handler = MyHandler() observer = Observer() observer.schedule(event_handler, path='.', recursive=False) observer.start() try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join()
When you run this program and make any changes to any file in current directory, the on_modified function from MyHandler class gets called with the event. In the MyHandler class you can define your own functions to handle the events. In the path, you can specify the files/directories you want to monitor. To stop this program, use Ctrl + C
- Related Questions & Answers
- How to setup VIM autoindentation properly for editing Python files?
- Python Support for gzip files (gzip)
- Psensor graphical hardware temperature monitor for linux
- How to Monitor Collections in Postman?
- How to convert PDF files to Excel files using Python?
- Program to find minimum changes required for alternating binary string in Python
- How to import other Python files?
- How to compare files in Python
- How to remove swap files using Python?
- How to create powerpoint files using Python
- How to Crack PDF Files in Python?
- How to monitor Network connections status in Android?
- How to Monitor Your Ubuntu System with Sysdig?
- How to monitor temporary tablespace usage in Oracle?
- Methods for tracking database schema changes in MySQL?
Advertisements