简体   繁体   English

Python:ConfigParser.NoSectionError:没有部分:'TestInformation'

[英]Python: ConfigParser.NoSectionError: No section: 'TestInformation'

I am getting ConfigParser.NoSectionError: No section: 'TestInformation' error using the above code. 我收到ConfigParser.NoSectionError:没有部分:使用上面的代码'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. 在app.cfg文件中。 Not sure what I am doing wrong 不确定我做错了什么

Use the readfp() function rather than read() since you are opening the file before reading it. 使用readfp()函数而不是read()因为在读取之前打开文件。 See Official Documentation . 参见官方文档

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM