Notifications
Clear all
Topic starter 17/08/2025 6:13 pm
AI-powered Anti-Virus system with real-time directory monitoring using Python’s watchdog
library. This will allow the system to automatically scan new or modified files in a specified folder and quarantine them if they’re flagged as malicious.
🗂 Updated Folder Structure
ai_antivirus/
├── data/
│ └── file_samples.csv
├── models/
│ └── av_model.pkl
├── quarantine/
├── test_files/
├── src/
│ ├── __init__.py
│ ├── data_loader.py
│ ├── feature_extractor.py
│ ├── model_trainer.py
│ ├── scanner.py
│ ├── quarantine.py
│ └── monitor.py
├── main.py
└── README.md
📄 File: src/monitor.py
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import os
from src.scanner import scan_file
WATCH_DIR = "test_files"
class FileEventHandler(FileSystemEventHandler):
def on_created(self, event):
if not event.is_directory:
print(f"📁 New file detected: {event.src_path}")
scan_file(event.src_path)
def on_modified(self, event):
if not event.is_directory:
print(f"✏️ File modified: {event.src_path}")
scan_file(event.src_path)
def start_monitoring():
print(f"🕵️ Monitoring directory: {WATCH_DIR}")
event_handler = FileEventHandler()
observer = Observer()
observer.schedule(event_handler, WATCH_DIR, recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
📄 Updated main.py
from src.data_loader import load_data
from src.model_trainer import train_model
from src.monitor import start_monitoring
DATA_PATH = "data/file_samples.csv"
MODEL_PATH = "models/av_model.pkl"
# Step 1: Train model
X, y = load_data(DATA_PATH)
train_model(X, y, MODEL_PATH)
# Step 2: Start real-time monitoring
start_monitoring()
📦 Installation Requirement
Make sure to install watchdog
:
pip install watchdog
🛡️ How It Works
- Monitors the
test_files/
directory. - Automatically scans any new or modified file.
- Quarantines malicious files to the
quarantine/
folder.