I have created a simple app that looks into a certain folder and concatenates all files dependent on the user input (start; date1 and end date; date2). Here is a snippet of the code below;
root = Tk()
# main function to create and save a csv file
def csv(date1, date2, path_1, path_2):
extension = 'txt'
daterange = pd.date_range(date1, date2).tolist()
daterange_list = [date.strftime('%d-%b-%Y') for date in daterange]
progress['value'] = 5
root.update_idletasks()
time.sleep(5)
all_filenames = []
for i in range (len (daterange_list)):
for j in glob.glob('{}2*{}*.{}'.format(path_1, daterange_list[i], extension)):
all_filenames.append(j)
progress['value'] = 30
root.update_idletasks()
time.sleep(5)
csv_file = pd.concat([pd.read_csv(f, delimiter='\t') for f in all_filenames])
progress['value'] = 60
root.update_idletasks()
time.sleep(5)
csv_file.to_csv(os.path.join(path_2, '{}{}{}to{}{}{}.csv'.format(date1[0:2], date1[3:6], date1[-4:], date2[0:2], date2[3:6], date2[-4:])), index = False)
progress['value'] = 100
root.update_idletasks()
time.sleep(5)
# select directory that contain all the text files to concatenate
def mydir():
global work_dir
work_dir = askdirectory(title='Select Working Directory Folder')
myLabel = Label(root, text = work_dir)
myLabel.grid(row = 0, column = 1)
# select directory to save the file
def mydir2():
global down_dir
down_dir = askdirectory(title='Select Download Folder')
myLabel = Label(root, text = down_dir)
myLabel.grid(row = 1, column = 1)
global progress
# create buttons and user input areas
button_dir = Button(root, text = "Select Battery Directory:", command = mydir)
button_down = Button(root, text = "Select Download Directory:", command = mydir2)
start_date = Entry(root, width = 20)
end_date = Entry(root, width = 20)
submit_button = Button(root, text = "Submit", command = lambda: csv(start_date.get(), end_date.get(), work_dir, down_dir))
progress = Progressbar(root, orient = HORIZONTAL, length=100, mode='determinate')
# create button layout
.........
root.mainloop()
The gui works perfectly for small amount of text files to concatenate, however if the number of files exceeds 30 files, the gui becomes unresponsive however the code still runs in the background.
I was thinking about implementing threads but i dont think it would help in this case? Especially if there are no background processes to be run.
Is there something that can be done to resolve this issue? Any help would be heaps appreciated as I am quite new to python.