Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 配置解析器正在打开文件但不返回任何内容?_Python_Python 3.x_Configparser - Fatal编程技术网

Python 配置解析器正在打开文件但不返回任何内容?

Python 配置解析器正在打开文件但不返回任何内容?,python,python-3.x,configparser,Python,Python 3.x,Configparser,我正试图像平常一样使用配置解析器,但出于某种原因,我没有拉取任何部分 我的代码: 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.C

我正试图像平常一样使用配置解析器,但出于某种原因,我没有拉取任何部分

我的代码:

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
[]
我的配置文件:

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
[]
代码输出:

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()”上没有错误,只返回一个空列表?我很困惑,我相信这是我错过的很简单的东西。。。任何帮助都将不胜感激。

这是因为您只有默认部分。根据该文件:

返回可用部分的列表;默认部分不包括在列表中。

然后,您不必打开该文件。配置解析器将为您完成这项工作

##设置配置解析器对象
config=configparser.configparser()
##将文件读入配置解析器
config.read(文件)
打印(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

导入配置解析器
config=configparser.configparser()
config.read('config.ini')
打印(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())

我尝试了多个部分。最初我有[MYSQL],同样的问题也发生了。我将更新帖子以显示这一点,而不是默认设置。编辑:我将配置文件改回[MYSQL],同样的问题也发生了。当我有默认的部分时,我也有[MYSQL]部分,但只想在文章中显示[MYSQL]部分,但被错拍了。感谢您的输入。我最初使用的是读取,但它不起作用,所以我改为读取文件,认为这可能是问题所在。该文件是正确的,因为当我直接阅读该文件时,我看到了正确的信息。我尝试了注释中的一个,它工作正常。你能编辑你的帖子并发布整个配置文件吗?帖子用我当前失败的配置文件更新。真奇怪。你的代码肯定在我的机器上工作。因为配置与脚本位于同一文件夹中。只需输入文件名并尝试一下。让我们看看。您是在代码还是主文件中导入了配置文件
从packagename导入配置
@AnmolParida我直接导入配置文件,而不是主文件。我检索当前文件的路径,并将配置文件名附加到路径字符串中,以输入到open中。我打印了文件路径,因为我认为可能是这样,但它是正确的。当我正常地通过python阅读文件时,它会显示内容。希望这有助于@AnmolParida感谢链接,但遗憾的是它没有。你能将其更改为
development.cfg
和tryAs吗?我在上面的评论中提到它最初是cfg,我将其更改为INI,因为我认为这是问题所在。我把它改回cfg进行测试,得到了相同的结果。