ConfigParser .set 未按预期工作

ConfigParser .set not working as expected

我无法理解为什么我的代码没有按预期工作。我想在第一次启动程序时选择两个数据集的路径。

#导入

import tkinter as tk
import pandas as pd
from configparser import ConfigParser
from tkinter import filedialog
from tkinter.messagebox import showerror, showinfo

#更新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)

#更新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)

#读取配置文件

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:
        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()

运行 第一次用下面的代码 confing.ini

[path]
load_dataset1_path = dummy
load_dataset2_path = dummy

returns一个错误:

FileNotFoundError: [Errno 2] 没有那个文件或目录: 'dummy/dataset1.xlsx'

错误后关闭。

如果我再次 运行 代码,一切都按预期工作,没有任何异常。 .ini 文件具有每个数据集的正确路径。 为什么 .ini 文件没有在我第一次 运行 代码时更新?为什么它保留每个路径的旧值?

请注意,您在以下代码中使用了更新配置文件之前加载的parser,因此设置仍然是旧设置:

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")

我建议 return 在 load_dataset1_directory()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

然后通过以上两个函数的return值更新两个变量:

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")