I am trying to use config parser like I normally do but for some reason I am not pulling any of the sections?
My Code:
import os, configparser
## Get directory path
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + "\\development.ini"
## Setup config parser object
config = configparser.ConfigParser()
## Read file into config parser
with open(file_path) as file:
config.read_file(file)
print(config.sections())
My Config File:
[MYSQL]
Host = 192.168.1.11
Port = 3306
Username = server
Password = (Removed)
Database = server
Code Output:
[]
No errors and just an empty list is returned on "config.sections()"? I am confused and I am sure it is something quite simple I am missing... Any help would be appreciated.
This is because you only have the default section. According to the doc :
Then, you don't have to open the file. The config parser will do it for you.
Here's an example :
config.ini
:test.py
:Output: