ciao!
ho provato anche con sys.exit:
codice:
import sys
import time
import shutil
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from utils import get_first_file, count_csv
class MonitorFile(FileSystemEventHandler):
def __init__(self):
self.dir_semaforo = './test_semaforo'
self.file_semaforo = './test_semaforo/semaforo'
self.file_canc = './test_semaforo\semaforo'
self.dir_to_move = './test_file/'
self.dir_csv = './csv'
self.observer = Observer()
def crea_semaforo(self):
if count_csv(self.dir_csv) > 0:
file = get_first_file(self.dir_csv)
shutil.move('./csv/' + file, self.dir_to_move + file)
time.sleep(1)
f = open(self.file_semaforo, "a")
f.write("")
f.close()
else:
sys.exit("ESCO")
def run_observer(self):
self.observer.schedule(self, path=self.dir_semaforo, recursive=False)
self.observer.start()
try:
while (True):
time.sleep(1)
except (KeyboardInterrupt, SystemExit):
self.observer.stop()
time.sleep(1)
self.observer.join()
def on_deleted(self, event):
if event.src_path == self.file_canc:
if count_csv(self.dir_csv) > 0:
self.crea_semaforo()
else:
self.observer.stop()
print(count_csv(self.dir_csv)) # STAMPA ZERO -- CORRETTO
print("DA USCIRE") # STAMPA IL PRINT -- CORRETTO
sys.exit("ESCO") # NON STAMPA NULLA -- RIMASE APPESO
ti ho messo nei commenti il comportamente che ha.
in pratica il processo rimane appeso.