[英]Python: ConfigParser.NoSectionError: No section: 'TestInformation'
我收到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文件中。 不確定我做錯了什么
使用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')
如果跳過文件打開步驟而不是文件的完整路徑到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.