Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 configparser.NoSectionError:无节:';myvars';_Python_Python 3.x_Config - Fatal编程技术网

Python configparser.NoSectionError:无节:';myvars';

Python configparser.NoSectionError:无节:';myvars';,python,python-3.x,config,Python,Python 3.x,Config,我有一个配置文件/tools/inputs/masterinputs.txt,其中包含以下内容: [myvars] maplat: 32.636004 maplon: -115.437702 我使用configparser阅读它: import configparser config = configparser.ConfigParser() config.read("/tools/inputs/masterinputs.txt") maplat = float(

我有一个配置文件
/tools/inputs/masterinputs.txt
,其中包含以下内容:

[myvars]
maplat: 32.636004
maplon: -115.437702
我使用
configparser
阅读它:

import configparser

config = configparser.ConfigParser()              
config.read("/tools/inputs/masterinputs.txt")
maplat = float(config.get("myvars", "maplat"))
但我得到了这个错误信息:

root@Primerpi:/tools# python3 solarrobot7-core.py

Traceback (most recent call last):
  File "/usr/lib/python3.2/configparser.py", line 1109, in _unify_values
    sectiondict = self._sections[section]
KeyError: 'myvars'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "solarrobot7-core.py", line 33, in <module>
    maplat = float(config.get("myvars", "maplat"))
  File "/usr/lib/python3.2/configparser.py", line 771, in get
    d = self._unify_values(section, vars)
  File "/usr/lib/python3.2/configparser.py", line 1112, in _unify_values
    raise NoSectionError(section)
configparser.NoSectionError: No section: 'myvars'
root@Primerpi:/tools#python3 solarrobot7-core.py
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.2/configparser.py”,第1109行,在值中
sectiondict=自身。\u节[节]
KeyError:“myvars”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“solarrobot7 core.py”,第33行,在
maplat=float(config.get(“myvars”、“maplat”))
get中的文件“/usr/lib/python3.2/configparser.py”,第771行
d=自身值(节,变量)
文件“/usr/lib/python3.2/configparser.py”,第1112行,在值中
上升方向错误(第节)
configparser.NoSectionError:没有节:“myvars”
该文件包含
[myvars]
部分,它显然读取配置文件。因此,我不知道是什么导致了这个错误。

文档清楚地说明了这一点(我的重点):

文档字符串: 读取并解析文件名或文件名列表

无法打开的文件将被静默忽略;这是 设计为您可以指定潜在的 配置文件位置(例如当前目录、用户的 主目录、系统范围目录)和所有现有 将读取列表中的配置文件。单人间 也可以给出文件名

Python找不到该文件

在文件开头添加以下内容:

import os

assert os.path.exists('/tools/inputs/masterinputs.txt')
如果您通过这些行,您应该会看到输出,前提是文件包含显示的内容

顺便说一句,您可以使用:

maplat = config.getfloat('myvars', "maplat")

检索浮动。

“正如您看到的文件”-不,我不能,因为您的帖子中没有包含该文件。顺便说一句,
#行没有效果,它必须是文件中的第一行,没有注释,空白,之前没有任何内容。谢谢。我想我也包括在内了。我已经删除了以a#开头的第一行,并将再次运行它,看看会发生什么。再次感谢您的回复!我删除了这一行#masterinputs.txt,将其放在/tools/inputs/中,但错误输出保持不变。Mike,请尝试您的建议。我也喜欢你的“maplat=”建议。这让我想回去工作了搞定了!“blinkin”文件真的是masterinputs..txt,我的眼睛从来没有看到额外的“.”。感谢所有帮助过我的人!真是太好了。顺便说一句,如果它解决了你的问题,你可以给出一个答案。
maplat = config.getfloat('myvars', "maplat")