简体   繁体   English

ConfigParser 不会保存对 .ini 文件中的值所做的更改

[英]ConfigParser won't save changes made to values in .ini file

first up, please forgive me if I'm making an obvious mistake, I'm very new to this lol.首先,如果我犯了一个明显的错误,请原谅我,我对这个大声笑很陌生。

I've made a little trivia game for me and my friends and I'm trying to keep a scoreboard using Configparser.我为我和我的朋友们做了一个小游戏,我正在尝试使用 Configparser 来保持记分牌。 Currently, what I think I'm doing is when someone gets a question correct, I read the file called scoreboard.ini grab that person's score add 1 to it and then rewrite the file.目前,我认为我正在做的是当有人得到正确的问题时,我读取名为 scoreboard.ini 的文件,获取该人的分数加 1,然后重写文件。 My issue is that the changes to the file don't save.我的问题是对文件的更改不保存。 When I run it and later call the scores it gives me the original unedited version and opening the file itself also shows that nothing has changed.当我运行它并稍后调用分数时,它给了我原始的未编辑版本,打开文件本身也表明没有任何改变。

with open(r'/filepath/scoreboard.ini','r+') as files:
         config.read((r'/filepath/scoreboard.ini'))
         print(config.getint('scores','playerA')) ### returns 0 which is correct
         PlayerA_Score = config.getint('scores','PlayerA_Score') + 1
         print(PlayerA_Score) ### returns 1 - working correctly
with open(r'/filepath/scoreboard.ini','w+') as files:
         config.write(files)
         config.set('scores', 'playerA', PlayerA_Score)
         print(config.getint('scores', 'playerA')) ### also returns 1 - still working up to here
         os.rename('scoreboard.ini','scoreboard.ini')

So up to here, it seems to be working fine, when I print out the values.所以到这里为止,当我打印出这些值时,它似乎工作正常。 Yet if I open the scoreboard file it still has the original values.但是,如果我打开记分牌文件,它仍然具有原始值。

Also worth mentioning I'm renaming the file as the same thing cause that was one potential solution I saw suggested elsewhere, but it hasn't worked.另外值得一提的是,我将文件重命名为同一件事,因为这是我在其他地方看到的一种潜在解决方案,但它没有奏效。 Previously I was just closing the file at that point instead.以前我只是在那时关闭文件。

I've also tried having two files, scoreboard which I open and read the values from and then writing a new file called scoreboardA where I write the updated scores values to.我也尝试过拥有两个文件,即我打开并从中读取值的记分板,然后编写一个名为 scoreboardA 的新文件,我将更新的分数值写入其中。 Then renaming scoreboardA to scoreboard and renaming scoreboard to something else.然后将 scoreboardA 重命名为 scoreboard 并将 scoreboard 重命名为其他内容。 Another potential solution I saw online that didn't work and honestly just confused me.我在网上看到的另一个潜在解决方案不起作用,老实说只是让我感到困惑。

I'm really at my wits end with this one lol and only have like 3 weeks of coding under my belt so any help would be very appreciated.我真的对这个大声笑束手无策,而且我只有大约 3 周的编码时间,所以任何帮助都将不胜感激。 Apologies again if this is a simple question I couldn't find a solution anywhere.如果这是一个简单的问题,我在任何地方都找不到解决方案,再次道歉。

Other potentially relevant information:其他可能相关的信息:

  • I am doing this for a bot I'm making for discord using discord.py我正在为使用 discord.py 为 discord 制作的机器人执行此操作
  • I am working in Pycharm我在 Pycharm 工作
  • I am on a mac我在 Mac 上

If you need any other information please let me know.如果您需要任何其他信息,请告诉我。

Thanks!谢谢!

Finally realized what I was doing wrong.终于意识到我做错了什么。 It seems I misunderstood what config.write() does lol.看来我误解了 config.write() 的作用,哈哈。 I needed to open the file, change the values using set and then use write() to save that to my file.我需要打开文件,使用 set 更改值,然后使用 write() 将其保存到我的文件中。 My amended code is.我修改后的代码是。

with open(r'/filepath/scoreboard.ini','r+') as files:
     config.read((r'/filepath/scoreboard.ini'))
     print(config.getint('scores','playerA')) 
     PlayerA_Score = config.getint('scores','PlayerA_Score') + 1
     print(PlayerA_Score)
     config.set('scores', 'playerA', PlayerA_Score)
     print(config.getint('scores', 'playerA')) 
with open(r'/filepath/scoreboard.ini','w+') as files:
     config.write(files)

I might still be misunderstanding something but my code works so maybe this will fix it for anyone else having a similar issue: :)我可能仍然会误解一些东西,但我的代码可以工作,所以也许这会为其他有类似问题的人解决它::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM