-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.py
51 lines (43 loc) · 1.49 KB
/
watch.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
#!/usr/bin/python
import datetime
import os
import subprocess
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
def is_non_zero_file(fpath):
return os.path.isfile(fpath) and os.path.getsize(fpath) > 0
class Handler(FileSystemEventHandler):
def __init__(self, time = datetime.datetime.now()):
self.time = time
def on_modified(self, event):
time.sleep(1)
os.system("cls")
print(event.src_path)
if event.src_path == r'.\Wars.py':
print("called")
if is_non_zero_file(os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))),
"wars.py",
)):
print("\n")
print("Output: ")
print("\n")
subprocess.run(["python", event.src_path])
else:
open(
os.path.join(os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))), "notes.txt",
),
"w",
).close()
os.system("cls")
if __name__ == "__main__":
event_handler = Handler()
observer = Observer()
observer.schedule(event_handler, path = ".", recursive = False)
observer.start()
try:
while True:
time.sleep(5)
except KeyboardInterrupt:
observer.stop()
observer.join()