Sep-09-2020, 03:21 AM
The following code works. It writes to disk all 17 files I an trying to write to file. Problem is, only the last record in each file is being saved. I could use some help.
# DEFINE LOAD RACE FILE FUNCTION def load_racefiles() : global Race_date global master_race_file global track_name global order global h global line field = ['track','race_number','todays_surface'] order= [] track_name = " " master_race_file = [] h = [] count = 0 df_filespath = ('C://guis/f_racefiles/') rows = 100 columns = 100 horse_array = [0 for i in range(rows)] filename = StringVar() f_name = StringVar() line = () fd = open("dirpath", "r") file_dir = (fd.read()) fd.close() os.chdir(file_dir) print(file_dir) for f in os.listdir(file_dir): file_name, file_ext = os.path.splitext(f) f_name = file_name + file_ext file_name = file_name.strip()[3:] line_counter = 0 if file_name == Race_date and file_ext == ".jcp": with open(f_name, "r") as csv_file: csv_reader = csv.reader(csv_file) for line in csv_reader: h = {'track' : line[0], 'race_number' : line[2], 'todays_surface' : line[6], } track_name = line[0] #os.chdir(df_filespath) filename = df_filespath + track_name + '.csv' with open(filename, 'a') as csv_file: d_writer = csv.DictWriter(csv_file, fieldnames = field) d_writer.writerow(h) my_labeldone= Label( text ="Downloading Complete", fg= "black", font = ("sans_serif" , 16)).place(x=500, y=500)Thank You