Python:ConfigParser.NoSectionError:无部分:'TestInformation'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16601757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: ConfigParser.NoSectionError: No section: 'TestInformation'
提问by Loganswamy
I am getting ConfigParser.NoSectionError: No section: 'TestInformation' error using the above code.
我使用上述代码收到 ConfigParser.NoSectionError: No section: 'TestInformation' 错误。
def LoadTestInformation(self):
config = ConfigParser.ConfigParser()
print(os.path.join(os.getcwd(),'App.cfg'))
with open(os.path.join(os.getcwd(),'App.cfg'),'r') as configfile:
config.read(configfile)
return config.items('TestInformation')
The file path is correct, I have double checked. and the config file has TestInformation section
文件路径是正确的,我已经仔细检查过。并且配置文件有 TestInformation 部分
[TestInformation]
IEPath = 'C:\Program Files\Internet Explorer\iexplore.exe'
URL = 'www.google.com.au'
'''date format should be '<Day> <Full Month> <Full Year>'
SystemDate = '30 April 2013'
in a app.cfg file. Not sure what I am doing wrong
在 app.cfg 文件中。不知道我做错了什么
采纳答案by RedBaron
Use the readfp()function rather than read()since you are opening the file before reading it. See Official Documentation.
使用该readfp()函数而不是read()因为您在读取文件之前打开文件。请参阅官方文档。
def LoadTestInformation(self):
config = ConfigParser.ConfigParser()
print(os.path.join(os.getcwd(),'App.cfg'))
with open(os.path.join(os.getcwd(),'App.cfg'),'r') as configfile:
config.readfp(configfile)
return config.items('TestInformation')
You can continue to use read()if you skip the file opening step and instead the full path of the file to the read()function
read()如果您跳过文件打开步骤,而是使用文件的完整路径,则可以继续使用该read()函数
def LoadTestInformation(self):
config = ConfigParser.ConfigParser()
my_file = (os.path.join(os.getcwd(),'App.cfg'))
config.read(my_file)
return config.items('TestInformation')

