'How do I stop the execution of watchdog after file has been found?
I have the following code written to search for all csv files in a file path and print the first csv file. However, my code keeps running even after the watchdog has executed the action. How do I get the watchdog to stop once it finds and prints the set file.
Essentially, I want the code to quit running if the file is found and read.
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s')
path = "C:/Users/Desktop/Test/"
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
# convert 1st csv in list to pandas df then print
first_csv = pd.read_csv(all_filenames[0])
print(first_csv)
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
#for starting the observer thread
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
