Python ConfigParser 问题 [英] Python ConfigParser Question
问题描述
ConfigParser 的配置文件是否必须命名为Config.ini"才能工作?
我希望名称为1Config.ini",以便它出现在文件夹目录的顶部.
这是我目前拥有的
config = ConfigParser.ConfigParser()config.read(Revision[0:Revision.rfind('\\')] + "\1Config.ini")Type = config.get("myvars", "Type")
但是当文件和代码命名为1Config.ini"时,我收到此错误
:没有部分:'myvars'
下面的输出是什么?确保它是一个有效的文件名.
<预><代码>>>>打印修订版[0:Revision.rfind('\\')] + "\1Config.ini"最好使用 os.path.join
而不是连接字符串:
导入操作系统文件名 = os.path.join(Revision[0:Revision.rfind('\\')], "Config.ini")config.read(文件名)
您可能不应该将变量命名为 Type
,因为 type
是一个内置函数/模块,它会令人困惑.
Type = config.get("myvars", "Type")
不,配置文件可以任意命名:
<预><代码>>>>a = ConfigParser.ConfigParser()>>>a.read("E:/Documents/2012/config.test") # 其中 config.test 是文档中的示例['E:/Documents/2012/config.test']>>>a.sections()['我的部分']>>>a.items(a.sections()[0])[('foodir', 'frob/whatever'),('dir', 'frob'),('long', '这个值在下一行继续\nin')]Does the Config file for the ConfigParser have to be named "Config.ini" in order to work?
I want the name to be "1Config.ini" so that it appears at the top of a folder dir.
This is what I have currently
config = ConfigParser.ConfigParser()
config.read(Revision[0:Revision.rfind('\\')] + "\1Config.ini")
Type = config.get("myvars", "Type")
I get this error however when the file and code is named "1Config.ini"
<class 'ConfigParser.NoSectionError'>: No section: 'myvars'
What's the output of the following? Make sure it's a valid file name.
>>> print Revision[0:Revision.rfind('\\')] + "\1Config.ini"
Ideally use os.path.join
instead of concatenating strings:
import os
filename = os.path.join(Revision[0:Revision.rfind('\\')], "Config.ini")
config.read(filename)
You probably shouldn't name your variable Type
, because type
is a built-in function/module and it'd be confusing.
Type = config.get("myvars", "Type")
And no, config files can be named anything:
>>> a = ConfigParser.ConfigParser()
>>> a.read("E:/Documents/2012/config.test") # where config.test is the example from the documentation
['E:/Documents/2012/config.test']
>>> a.sections()
['My Section']
>>> a.items(a.sections()[0])
[('foodir', 'frob/whatever'),
('dir', 'frob'),
('long', 'this value continues\nin the next line')]
这篇关于Python ConfigParser 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!