I want to run my function whenever a change occurs in my folder. The main thing to do here is to normalize new files, we do so by comparing my two folders, the folder that contains the normalized ones and the folder where we add new files and return a list of the unmatched files which our normalize function will modify and place in the folder of normalized ones.
Here is my code:
def normalize(c):
PATH = path_to_file
fileNames = os.listdir(PATH)
fileNames = c
for file in fileNames:
sfm = pd.read_csv(PATH + file, delimiter = ';', parse_dates=[[0, 1]], skiprows = 3)
# other instructions then rewrite my new dataframe in my normalized folder
sfm.to_csv('.\dirnormalized'+str(file[:-4])+'.csv', sep=';', index=False, header=True)
def comparison():
PATH = path_to_file
PATH_norm = path_to_norm
fileNames = os.listdir(PATH)
files = os.listdir(PATH_norm)
fileNames = [file for file in fileNames if '.csv' in file]
files = [fl for fl in files if '.csv' in fl]
diff = [x for x in fileNames if x not in files]
return diff
def change():
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
# path_to_file and word_file are the same the only difference is that in path_to_file we have a back slash in the end in order to acces the files one by one
path = word_file
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def process():
if(change()):
c = comparison()
normalize(c)
if __name__ == "__main__":
import os
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
process()
The only problem here is that in my process() function the if() instruction doesn't seem pass to other ones, I can see when the new files are added which means when a modification occurs but the other instructions don't occur it stucks on if().
returnin your functionchange(). So the lineif(change()):in yourprocess()function is actually alwaysif(None):so the code in that statement never runs.