0
import sys
import time
import logging, os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class Watcher:

    DirectoryToWatch = 'C:\\Users\\dbeiler\\Documents\\Visual Studio 2015\\Projects\\New folder\\'

    def __init__(self):
        self.observer = Observer()
    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DirectoryToWatch, 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:
            return None

        elif event.event_type == 'modified':
            # Take any action here when a file is first created.
            os.system('python writetodata.py')
            sys.exit
            time.sleep(5)


if __name__ == '__main__':
    w = Watcher()
    w.run()

The application works when i copy a file to the location, it will execute my script, however the script keeps cycling, i would like it to stop and wait for another file once its already been written, not sure what im missing

I moved one of my directory folders, and now its writing to my output excel file twice, i assume its because 1.) its being opened, and 2.) its saving.

3
  • I need a loop of some sort Commented Mar 20, 2017 at 17:58
  • The observer already "loops". It is continually checking the specified directory for changes. Commented Mar 21, 2017 at 13:33
  • Yeah, im having the same issue as this guy stackoverflow.com/questions/21587415/… Commented Mar 21, 2017 at 14:49

1 Answer 1

1

how about looking for a specific object type only like xlsx'?

Sign up to request clarification or add additional context in comments.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.