Also includes source code that is not directly related to the cause of the problem, as a memorandum of your own.
When I try to read an ini file with python,
For some reason, I get a KeyError
and it doesn't work.
The source is as follows
config.ini
[USER_INFO]
address=testUser
password=testpass
test.py
import configparser
config = configparser.ConfigParser()
config.read('./config.ini', encoding='utf-8')
address = config['USER_INFO']['address']
It doesn't look like the key value is wrong.
I thought that config.ini could not be read, so I checked the existence of the file. As a result, False was returned.
python
import os
print(os.path.exists('./config.ini'))
Getting the folder wasn't working.
When I ran the program, I was running it from VS Code,
The cause is that the current directory when executing was not the file in which test.py
exists.
When I moved to the directory where test.py
exists and then executed it, the value from the ini file was also successfully obtained.
Recommended Posts