2013-05-17 2 views
9

Я получаю ConfigParser.NoSectionError: Нет раздела: 'TestInformation' с использованием вышеуказанного кода.Python: 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') 

Путь к файлу верен, я дважды проверял. и конфигурационный файл имеет раздел 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' 

в файле app.cfg. Не уверен, что я делаю неправильно

+0

'app.cfg' или' App.cfg'? – RedBaron

+0

App.cfg. следует ли использовать только app.cfg? – Loganswamy

+0

В последней строке вашего вопроса вы говорите, что все это записано в 'app.cfg', но в вашем коде вы открываете' App.cfg'. Я возьму это как опечатку. – RedBaron

ответ

8

Используйте функцию readfp(), а не read(), так как вы открываете файл перед его чтением. См. 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') 

Вы можете продолжать использовать 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') 
Смежные вопросы