2013-05-17 3 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') 

ファイルパスは正しいですか、私は二重チェックしました。 configファイルには、Test.cfgファイルの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' 

があります。私が間違っていることがわからない

+0

'app.cfg'または' App.cfg'に代わりにファイルのフルパスをファイル開くステップを省略している場合はread()を使い続けることができますか? – RedBaron

+0

App.cfg。 app.cfgだけを使うべきですか? – Loganswamy

+0

あなたの質問の最後の行では、すべてを 'app.cfg'に入れましたが、あなたのコードでは' App.cfg'を開いています。私はタイプミスとしてそれを取るでしょう。 – RedBaron

答えて

8

read()より読むのではなく、readfp()関数を使用してください。読む前にファイルを開いています。 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()機能

def LoadTestInformation(self):   
    config = ConfigParser.ConfigParser()  
    my_file = (os.path.join(os.getcwd(),'App.cfg')) 
    config.read(my_file) 
    return config.items('TestInformation') 
関連する問題