Currently I have a folder observer that basically checks whether a new file is added and then should run another Python script for some other executions. The other python script basically transforms a csv into an Excel Table format. The problem is that the first run/try it does everything it supposed to after a file enters the folder, but whenever a second file is added.... it just ignores to run the other Python script. What am I missing?
The observer:
def on_created(event):
print(f"Hey, {event.src_path} has been created!\n")
print(f"The other Python file will now be run\n")
time.sleep(5)
import TableRun
def on_deleted(event):
print(f"What the f**k! Someone deleted {event.src_path}!\n")
def on_modified(event):
print(f"Hey hey hey, {event.src_path} has been modified \n")
def on_moved(event):
print(f"Ok ok ok, someone moved {event.src_path} to {event.dest_path}")
if __name__ == "__main__":
patterns = "*"
ignore_patterns = ""
ignore_directories = False
case_sensitive = True
my_event_handler = PatternMatchingEventHandler(patterns, ignore_patterns, ignore_directories, case_sensitive)
my_event_handler.on_created = on_created
my_event_handler.on_deleted = on_deleted
my_event_handler.on_modified = on_modified
my_event_handler.on_moved = on_moved
path = str(current_path+"String of path")
go_recursively = True
my_observer = Observer()
my_observer.schedule(my_event_handler, path, recursive=go_recursively)
my_observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
my_observer.stop()
my_observer.join()```
A file only ever gets imported once. The first time you run import TableRun
the code loads and executes, but every other time you hit that statement it's a no-op.
The proper solution is to move your import
statement to the top of your code, rewrite TableRun
so that it does not execute code on import
, and replace your existing import
statement with a function call. For example:
import TableRun
def on_created(event):
print(f"Hey, {event.src_path} has been created!\n")
print(f"The other Python file will now be run\n")
time.sleep(5)
TableRun.do_something_with_the_file()