配置解析器打开文件但不返回任何内容?
Config parser opening file but not returning anything?
我正在尝试像往常一样使用配置解析器,但出于某种原因我没有提取任何部分?
我的代码:
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())
我的配置文件:
[MYSQL]
Host = 192.168.1.11
Port = 3306
Username = server
Password = (Removed)
Database = server
代码输出:
[]
“config.sections()”没有错误,只返回一个空列表?我很困惑,我确信这是我所缺少的非常简单的东西...任何帮助将不胜感激。
这是因为您只有默认部分。根据文档:
Return a list of the sections available; the default section is not included in the list.
https://docs.python.org/3/library/configparser.html
那么,您不必打开该文件。配置解析器将为您完成。
## Setup config parser object
config = configparser.ConfigParser()
## Read file into config parser
config.read(file)
print(config.sections())
这是一个例子:
config.ini
:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
test.py
:
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
print(config.sections())
输出:
['bitbucket.org', 'topsecret.server.com']
我猜问题只出在访问文件上。我尝试使用以下代码将两个文件放在一个文件夹中,运行正常。尝试更改为 .cfg
from configparser import ConfigParser
config = ConfigParser()
config.read("dev.cfg")
print(config.sections())
或您的代码
import os, configparser
## Get directory path
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + "\dev.cfg"
## 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())
我正在尝试像往常一样使用配置解析器,但出于某种原因我没有提取任何部分?
我的代码:
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())
我的配置文件:
[MYSQL]
Host = 192.168.1.11
Port = 3306
Username = server
Password = (Removed)
Database = server
代码输出:
[]
“config.sections()”没有错误,只返回一个空列表?我很困惑,我确信这是我所缺少的非常简单的东西...任何帮助将不胜感激。
这是因为您只有默认部分。根据文档:
Return a list of the sections available; the default section is not included in the list. https://docs.python.org/3/library/configparser.html
那么,您不必打开该文件。配置解析器将为您完成。
## Setup config parser object
config = configparser.ConfigParser()
## Read file into config parser
config.read(file)
print(config.sections())
这是一个例子:
config.ini
:
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
[bitbucket.org]
User = hg
[topsecret.server.com]
Port = 50022
ForwardX11 = no
test.py
:
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
print(config.sections())
输出:
['bitbucket.org', 'topsecret.server.com']
我猜问题只出在访问文件上。我尝试使用以下代码将两个文件放在一个文件夹中,运行正常。尝试更改为 .cfg
from configparser import ConfigParser
config = ConfigParser()
config.read("dev.cfg")
print(config.sections())
或您的代码
import os, configparser
## Get directory path
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path + "\dev.cfg"
## 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())