-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.py
More file actions
40 lines (34 loc) · 1.2 KB
/
watch.py
File metadata and controls
40 lines (34 loc) · 1.2 KB
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
import time
import subprocess
import platform
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def __init__(self):
self.last_executed = 0
self.debounce = 0 # Adjust the debounce time as needed (in seconds)
def on_modified(self, event):
current_time = time.time()
if current_time - self.last_executed > self.debounce and event.src_path.endswith("test.py"):
print(f"Detected change in {event.src_path}")
clear_terminal()
subprocess.run(["python", event.src_path])
self.last_executed = current_time
def clear_terminal():
# Clear terminal based on platform (Windows/Linux/Mac)
system_platform = platform.system()
if system_platform == "Windows":
subprocess.run("cls", shell=True)
else:
subprocess.run("clear", shell=True)
if __name__ == "__main__":
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()