I'm having trouble understanding why my code isn't working as expected. I want to choose, as I boot up for the first time my program, the path each of two datasets.
#Import
import tkinter as tk
import pandas as pd
from configparser import ConfigParser
from tkinter import filedialog
from tkinter.messagebox import showerror, showinfo
#Function to update the path for dataset1
def load_dataset1_directory():
load_dataset1_dir= filedialog.askdirectory()
parser = ConfigParser()
parser.read("config.ini")
parser.set("path", "load_dataset1_path", load_dataset1_dir)
with open("config.ini","w") as configfile:
parser.write(configfile)
#Function to update the path for dataset2
def load_dataset2_directory():
load_dataset2_dir= filedialog.askdirectory()
parser = ConfigParser()
parser.read("config.ini")
parser.set("path", "load_dataset2_path", load_dataset2_dir)
with open("config.ini","w") as configfile:
parser.write(configfile)
#Read config file
try:
parser = ConfigParser()
parser.read("config.ini")
saved_load_dataset1_path = parser.get("path", "load_dataset1_path")
saved_load_dataset2_path = parser.get("path", "load_dataset2_path")
except:
showerror(
title='Error',
message='Error 0: Configuration File not found')
#root window
root = tk.Tk()
root.geometry("800x500")
root.resizable(True, True)
root.title('Prg1')
#Try getting datasets
try:
df_1 = pd.read_excel(saved_load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(saved_load_dataset2_path "/data2.xlsx")
except:
showerror(
title='Error',
message='Error 1: Data not found')
showinfo(
title='Select Data',
message='Select Dataset1')
load_dataset1_directory()
showinfo(
title='Select Data',
message='Select Dataset2')
load_dataset2_directory()
saved_load_dataset1_path = parser.get("path", "load_dataset1_path")
saved_load_dataset2_path = parser.get("path", "load_dataset2_path")
df_1 = pd.read_excel(saved_load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(saved_load_dataset2_path "/data2.xlsx")
root.mainloop()
Running the code for the first time with the following confing.ini
[path]
load_dataset1_path = dummy
load_dataset2_path = dummy
returns an error:
FileNotFoundError: [Errno 2] No such file or directory: 'dummy/dataset1.xlsx'
After the error it shut down.
If I run the code again everything works as expected without any exception. The .ini file has the correct path for each dataset. Why the .ini file is not updating the first time I run the code? Why does it retains the old values for each path?
CodePudding user response:
Note that you have used the parser
loaded before updating the config file in the following code, so the settings are still the old ones:
try:
parser = ConfigParser()
parser.read("config.ini")
saved_load_dataset1_path = parser.get("path", "load_dataset1_path")
saved_load_dataset2_path = parser.get("path", "load_dataset2_path")
except:
showerror(
title='Error',
message='Error 0: Configuration File not found')
...
try:
df_1 = pd.read_excel(saved_load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(saved_load_dataset2_path "/data2.xlsx")
except:
showerror(
title='Error',
message='Error 1: Data not found')
showinfo(
title='Select Data',
message='Select Dataset1')
load_dataset1_directory()
showinfo(
title='Select Data',
message='Select Dataset2')
load_dataset2_directory()
# parser is the already loaded one in the above try/except block which contains the old content of the configuration file
saved_load_dataset1_path = parser.get("path", "load_dataset1_path") # get 'dummy'
saved_load_dataset2_path = parser.get("path", "load_dataset2_path") # get 'dummy'
df_1 = pd.read_excel(saved_load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(saved_load_dataset2_path "/data2.xlsx")
I would suggest to return the selected folder in load_dataset1_directory()
and load_dataset2_directory()
:
def load_dataset1_directory():
load_dataset1_dir= filedialog.askdirectory()
parser = ConfigParser()
parser.read("config.ini")
parser.set("path", "load_dataset1_path", load_dataset1_dir)
with open("config.ini","w") as configfile:
parser.write(configfile)
return load_dataset1_dir
def load_dataset2_directory():
load_dataset2_dir= filedialog.askdirectory()
parser = ConfigParser()
parser.read("config.ini")
parser.set("path", "load_dataset2_path", load_dataset2_dir)
with open("config.ini","w") as configfile:
parser.write(configfile)
return load_dataset2_dir
Then update the two variables by the return values of the above two functions:
try:
df_1 = pd.read_excel(load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(load_dataset2_path "/data2.xlsx")
except:
showerror(
title='Error',
message='Error 1: Data not found')
showinfo(
title='Select Data',
message='Select Dataset1')
saved_load_dataset1_path = load_dataset1_directory()
showinfo(
title='Select Data',
message='Select Dataset2')
saved_load_dataset2_path = load_dataset2_directory()
#saved_load_dataset1_path = parser.get("path", "load_dataset1_path")
#saved_load_dataset2_path = parser.get("path", "load_dataset2_path")
df_1 = pd.read_excel(saved_load_dataset1_path "/data1.xlsx")
df_2 = pd.read_excel(saved_load_dataset2_path "/data2.xlsx")