I have a function that should clean up csv files included on my list:
fileListLocalDaily = (glob.glob("/path/to/my/directory/*.csv") for y in fileListLocalDaily: data = y def prepare_file(y): data = y lines = pd.read_csv(data, sep=",", quoting=csv.QUOTE_NONE) new_lines = lines.replace('something', '', regex=True) f = StringIO(data) # extract date date = next(f) date = date.split('_')[1] date = os.path.splitext(date)[0] new_lines.insert(2,'date',date) new_lines.drop(new_lines.columns[0:2], axis=1, inplace=True) new_lines.drop(new_lines.columns[6], axis=1, inplace=True) new_lines=new_lines.sort_values(by=['Something'], ascending=False) new_lines.to_csv('/path/to/my/output/'+date+'.csv', index = False) complete = prepare_file(data) runFunction = prepare_file(y) It seems that the above function saved only one file and kept overwriting it over and over in an endless loop. Could someone help me understand how could I run this function to all csv files in my directory one after one? thanks
date, which would cause the output file to be the same? Have you considered using the name of the input file to determine what to name the output file, since you know that all the input files have distinct names?runFunction = prepare_file(y)should it inside theforloop. Also, move the function itself outside the loop.