将文件作为配置写入时出错(需要整数)

2024-04-20 04:40:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用ini文件作为配置文件。在programmstart上,我读取ini文件,当程序运行时,我将更改文件中的值(例如日期)。下面是我读取ini文件的代码:

inifile = r"C:\Users\A52113242\Desktop\PROJEKTE\[INPROGRESS] AUSWERTUNG COIN + HVL\_tool\cfg\cfg.ini"

config = configparser.SafeConfigParser()
config.read(inifile)

date_hvl = config.get('date', 'date_hvl')
date_coin = config.get('date', 'date_coin')
set_date = time.strftime("%d.%m.20%y")

下面是我想更改“date”值的函数:

def coin_daten_erstellen():
config.set('date', 'date_coin', set_date)
config.write(sys.stdout)
with open(inifile,'w') as configfile:
    config.write(configfile)

我得到的错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\A52113242\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\A52113242\Desktop\PROJEKTE\[INPROGRESS] AUSWERTUNG COIN + HVL\_tool\tool.py", line 179, in coin_daten_erstellen
    with open(inifile,'w') as configfile:
TypeError: an integer is required (got type str)

我做错什么了?我不明白为什么需要整数。 此外,ini文件结构如下:

[date]
date_coin = 10.07.2017
date_hvl = 10.07.2017

谢谢你的帮助。你知道吗


Tags: 文件inconfigdatetoolusersinicoin
1条回答
网友
1楼 · 发布于 2024-04-20 04:40:40

该消息表明它读取目录中的“+”就像一个加法,而不是目录的一部分。你知道吗

也许你应该在不改变代码的情况下重试,但是使用一个没有空格和“+”的路径?你知道吗

示例: r“C:\Users\A52113242\Desktop\PROJEKTE\INPROGRESS\u AUSWERTUNG\u COIN\u HVL\u tool\cfg\cfg.ini文件““

相关问题 更多 >