1

I want to start some python codes after another software.

This software do some data extractions and gives me periodically some results in CVS file form (weekly). What I want to do is to trigger my Python code to be executed on these file in order to do some functions with them and I want this to be done after every extraction.

I have tried while(1) but it doesn't seem to be efficient.

Is there any Python codes or a software to execute the Python that I can use to solve this problem? Thanks in advance.

2
  • 1
    From what i get from your question you could call the python script from the software doing the extraction? If you can not do that, you could maybe script a check on the files modified time and call that script every X minutes in crontab? Commented May 28, 2019 at 15:05
  • yes that s what i want Commented May 29, 2019 at 7:12

1 Answer 1

1

You can watch the folder then act on the files creation and/or modify events. Your script can be watching a remote drive on the network if needed.

The following is cut out of working code but I can't confirm it works in this snippet state. It'll call HandleNewlyCreated() for newly created files.

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class WatchPendingHandler(FileSystemEventHandler):
    ''' Run a handler for every file added to the pending dir

    This class also handles what I call a bug in the watchdog module
    which means that you can get more than one call per real event
    in the watched dir tree.
    '''

    def __init__(self):
        super(WatchPendingHandler, self).__init__()
        # wip is used to avoid bug in watchdog which means multiple calls
        # for one real event.
        # For reference: https://github.com/gorakhargosh/watchdog/issues/346
        self.wip = []

    def on_created(self, event):
        path = event.src_path
        if event.is_directory:
            logging.debug('WatchPendingHandler() New dir created in pending dir: {}'.format(path))
            return
        if path in self.wip:
            logging.debug('WatchPendingHandler() Dup created event for %s', path)
            return
        self.wip.append(path)
        logging.debug('WatchPendingHandler() New file created in pending dir: {}'.format(path))
        HandleNewlyCreated(path)

    def on_moved(self, event):
        logging.debug('WatchPendingHandler() %s has been moved', event.src_path)
        with contextlib.suppress(ValueError):
            self.wip.remove(event.src_path)

    def on_deleted(self, event):
        path = event.src_path
        logging.debug('WatchPendingHandler() %s has been deleted', path)
        with contextlib.suppress(ValueError):
            self.wip.remove(path)

observer = Observer()
observer.schedule(WatchPendingHandler(), DIR_PENDING, recursive=True)
observer.start()
logging.info('Watching %s', DIR_PENDING)
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    observer.stop()
observer.join()

Explanation of on_created(): It will check if the event was for the creation of a directory. If so it logs the event and then returns/ignores it. It then checks whether the file created event has previously happened (path in self.wip). If so, it also ignores the event and returns. Now it can record the event in self.wip, log the new event and then call HandleNewlyCreated() to process the newly created file.

If the file is moved or deleted, then we need to remove the path from self.wip so that the next created event for a new file won't be ignored.

More info here: https://pypi.org/project/watchdog/

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

2 Comments

Could you explain what on_created function does ?
I've updated the answer with detail but the short answer is it'll call HandleNewlyCreated() only for files and only once when they are created.

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.