Search code examples
pythonpython-multiprocessingpython-multithreadingpython-watchdog

Run the python script in a thread when a new file is created in a folder


I am using watchdog to monitor a folder and running my script to convert pcap files to csv as soon as new file is created in that folder,However I am able to do that but I want to run my function in a thread whenever a new file is added in a folder so that i can work on multiple files . Here's my code:

import os
import glob
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

def on_created(event):
    files_list = glob.glob('path/to/pcap/files')
    latest_file = max(files_list, key=os.path.getctime)
    create_csv(latest_file)


def create_csv(latest_file):
    x=f('tshark -r {latest_file} -Y sip -E header=y -E separator=, -T fields -e sip.From  -e sip.To > {latest_file}.csv ')
    print(x)
    os.system(x)
   

if __name__ == "__main__":
    event_handler = FileSystemEventHandler()
   
    event_handler.on_created = on_created

    path = 'path/to/pcap/files'

    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()

    observer.join()

Solution

  • To thread functions, you should use the built in threading module.

    For example:

    import threading
    
    def someFunction():
      pass
    
    threading.Thread(target=someFunction).start()
    

    In your code, you could implement it like this:

    import os
    import glob
    import time
    from watchdog.observers import Observer
    from watchdog.events import FileSystemEventHandler
    import threading
    
    def on_created(event):
        files_list = glob.glob('path/to/pcap/files')
        latest_file = max(files_list, key=os.path.getctime)
        create_csv(latest_file)
    
    
    def create_csv(latest_file):
        x=f('tshark -r {latest_file} -Y sip -E header=y -E separator=, -T fields -e sip.From  -e sip.To > {latest_file}.csv ')
        print(x)
       
        #Threading code:
        def thread_func():
          os.system(x)
        threading.Thread(target=thread_func).start()
    
    if __name__ == "__main__":
        event_handler = FileSystemEventHandler()
       
        event_handler.on_created = on_created
    
        path = 'path/to/pcap/files'
    
        observer = Observer()
        observer.schedule(event_handler, path, recursive=True)
        observer.start()
    
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
    
        observer.join()
    

    On another note, to pass arguments to a thread function:

    import threading
    
    def someFunction(someArg):
      pass
    
    x = 0
    
    threading.Thread(target=someFunction, args=[x]).start()
    

    Lastly, here's a link to the docs: https://docs.python.org/3/library/threading.html