-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirectory_watch.py
More file actions
87 lines (71 loc) · 2.63 KB
/
directory_watch.py
File metadata and controls
87 lines (71 loc) · 2.63 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os.path
from datetime import datetime
dir= "../files/"
logfile_name = dir + "log.txt"
#check if logfile exists
file_exists = os.path.exists(logfile_name)
if file_exists == False:
f= open(logfile_name, "w")
print("New logfile created in working directory")
else:
print("Log file already exists. Appending logs in same file")
logfile = open(logfile_name,'a')
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %I:%M:%S %p")
logfile.write("\n------------------------------------------------------\n")
logfile.write(f"Log generation date and time = {dt_string} \n")
logfile.write("------------------------------------------------------\n\n")
class Watcher:
DIRECTORY_TO_WATCH = dir
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
#print(event.src_path)
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
log = f"File created - {event.src_path} "
lognow = datetime.now()
print(log)
logfile.write(lognow.strftime("%I:%M:%S %p "))
logfile.write(log + '\n')
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
log = f"File modified -{event.src_path} "
lognow = datetime.now()
print(log)
logfile.write(lognow.strftime("%I:%M:%S %p "))
logfile.write(log + '\n')
elif event.event_type == 'deleted':
# Taken any action here when a file is deleted.
log = f"File deleted - {event.src_path} "
lognow = datetime.now()
print(log)
logfile.write(lognow.strftime("%I:%M:%S %p "))
logfile.write(log + '\n')
elif event.event_type == 'moved':
log = f"File moved - {event.src_path} to {event.dest_path} "
lognow = datetime.now()
print(log)
logfile.write(lognow.strftime("%I:%M:%S %p "))
logfile.write(log + '\n')
if __name__ == '__main__':
w = Watcher()
w.run()