微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 configparser 更新值 python .ini 文件

如何解决如何使用 configparser 更新值 python .ini 文件

我正在编写一个代码,我可以将所有其他应用程序放入其中(例如集线器)。我想将我的进度保存到 .ini 文件中。我使用了 .set 命令。我还在退出函数添加一个保存写入循环,但它不起作用。它显示错误

Traceback (most recent call last):
  File "C:\Users\User\Desktop\os\interface.py",line 20,in <module>
    apps = int(config['DEFAULT']['apps'])
  File "C:\Users\User\AppData\Local\Programs\Python\python39\lib\configparser.py",line 1254,in __getitem__
    raise KeyError(key)
KeyError: 'apps'

savefile.ini

[DEFAULT]
apps = blue
allapps = setings,powerbrowser
games = 0
allgames = 
coins = 0
image = 0
toolbar_color = black

interface.py

from tkinter import *
from tkinter import messageBox
import datetime
import calendar
import os.path
from configparser import ConfigParser
import time

root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes('-fullscreen',True)
config = ConfigParser()
config.read('savedata/savefile.ini')

Now = datetime.datetime.Now()
year = Now.year
month = Now.month
monthes = ['None','January','February','march','April','May','June','July','Augest','September','October','November','December']
apps = int(config['DEFAULT']['apps'])
allapps = config['DEFAULT']['allapps']
games = int(config['DEFAULT']['games'])
allgames = config['DEFAULT']['allgames']
coins = int(config['DEFAULT']['coins'])
time_Now = f'{monthes[month]},{year}'
background_image = ['bg/wallpaper0.png','bg/wallpaper1.png']
image = int(config['DEFAULT']['image'])
toolbar_color = config['DEFAULT']['toolbar_color']
ids = []



# Functions
def quit():
    global config,root
    MsgBox1 = messageBox.askquestion('Are you sure?','Are you sure you want to quit?',icon = 'warning')
    if MsgBox1 == 'yes':
        with open('savedata/savefile.ini','w') as configfile:
            config.write(configfile)
        root.destroy()

def startapp():
    global background
    background.forget()
def closeapp():
    global background
    background.pack(expand=True)


main = Frame(root)
xmain = -2
ymain = -2

# icons
shutdown_ico = PhotoImage(file='icons/shut_down.png')

backg = PhotoImage(file=background_image[image])
background = Label(main,image=backg)
tool = Frame(background,bg=toolbar_color,width=1870,height=50)
date = Label(tool,text=time_Now,fg='white',font='Arial 15 bold')
shutdown = Button(tool,image=shutdown_ico,highlightthickness=0,relief=FLAT,command=quit)
border1 = Frame(tool,bg='grey',height=45,width=2)
yborder1 = 2

main.place(x=xmain,y=ymain)
tool.place(x=0,y=815)
background.pack(expand=True)
date.place(x=1370,y=10)
border1.place(x=60,y=yborder1)
shutdown.place(x=12,y=5)




root.mainloop()

请帮帮我。也许有更好的方法。 (我英语不好所以可能会有拼写错误)。

解决方法

根据python documentation,代码的用法:

from configparser import ConfigParser

config = ConfigParser()
config.read('savedata/savefile.ini')
apps = int(config['DEFAULT']['apps'])

如果正在解析的文件有效,则正确。我建议输出数据并查看它是否正确解析:

from configparser import ConfigParser

config = ConfigParser()
config.read('savedata/savefile.ini')
print(config.sections())

输出应至少包含“DEFAULT”。如果没有,则说明解析不正确,因此请确保给定的文件名有效。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。