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

Log*_*amy 11 python configparser

我收到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')
Run Code Online (Sandbox Code Playgroud)

文件路径是正确的,我已经双重检查.并且配置文件具有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'
Run Code Online (Sandbox Code Playgroud)

在app.cfg文件中.不确定我做错了什么

Red*_*ron 8

使用该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')
Run Code Online (Sandbox Code Playgroud)

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')
Run Code Online (Sandbox Code Playgroud)