2013-05-17 14 views
9

Otrzymuję ConfigParser.NoSectionError: Brak sekcji: "TestInformation" błąd przy użyciu powyższego kodu.Python: ConfigParser.NoSectionError: Brak sekcji: "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') 

Ścieżka do pliku jest poprawna, dwukrotnie sprawdziłem. a plik konfiguracyjny ma sekcję 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' 

w pliku app.cfg. Nie jestem pewien, co robię źle

+0

'app.cfg' lub' App.cfg'? – RedBaron

+0

App.cfg. powinienem użyć tylko app.cfg? – Loganswamy

+0

W ostatnim wierszu twojego pytania mówisz, że umieściłeś to wszystko w "app.cfg", ale w twoim kodzie otwierasz 'App.cfg'. Przyjmę to jako literówkę. – RedBaron

Odpowiedz

8

Użyj funkcji readfp() zamiast read(), ponieważ otwierasz plik przed jego odczytaniem. Zobacz 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') 

Można nadal używać read() jeśli pominąć etap otwarcia pliku i zamiast pełnej ścieżki pliku do read() funkcji

def LoadTestInformation(self):   
    config = ConfigParser.ConfigParser()  
    my_file = (os.path.join(os.getcwd(),'App.cfg')) 
    config.read(my_file) 
    return config.items('TestInformation') 
Powiązane problemy