-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (46 loc) · 1.91 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Author: Bartu KILIÇ
# Contact: [email protected]
# GitHub: https://github.com/silexi
# Website: https://bartukilic.com
import os
import psutil
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
LOG_FILE = "process_log.txt"
class FileEventHandler(FileSystemEventHandler):
def __init__(self, file_to_watch):
self.file_to_watch = file_to_watch
def process(self, event):
if event.event_type == 'modified' or event.event_type == 'created':
if event.src_path.endswith(self.file_to_watch):
self.log_processes_accessing_file()
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
def log_processes_accessing_file(self):
with open(LOG_FILE, "a") as log_file:
log_file.write(f"\n[{time.ctime()}] {self.file_to_watch} accessed by:\n")
for proc in psutil.process_iter(['pid', 'name', 'open_files']):
try:
if any(f.path == self.file_to_watch for f in proc.info['open_files'] or []):
log_file.write(f"PID: {proc.info['pid']}, Name: {proc.info['name']}\n")
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def monitor_file(file_to_watch):
event_handler = FileEventHandler(file_to_watch)
observer = Observer()
observer.schedule(event_handler, path=os.path.dirname(file_to_watch), recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
file_to_watch = "mali_tablolar.xlsx"
if not os.path.isabs(file_to_watch):
file_to_watch = os.path.join(os.getcwd(), file_to_watch)
monitor_file(file_to_watch)