Python Forum

Full Version: configparser module, when use text file to show following error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import configparser

config = configparser.ConfigParser()
#config.read("E:\\My Folders\\Alnafi\\AlNafi\\configparser_Ex3.ini") # when we use .ini file then out put is ok
config.read_file("E:\\My Folders\\Alnafi\\AlNafi\\configparser_Ex3.txt")
config.sections()
Error:
MissingSectionHeaderError: File contains no section headers. file: '<???>', line: 1 'E'
-----------
A configparser file normally needs a section header
Output:
[mysection] foo = bar
Quote:#Thanks your reply
#this is my .txt file using in configparser module

[rootdirectory]
root_directory = e:\alnafi
[username]
user_name = m.bilal
[databaseconnected]
database_connected = True
[summaryunit]
summary_unit = False

Error:
File "C:\Users\Lenovo\Anaconda3\lib\configparser.py", line 1079, in _read raise MissingSectionHeaderError(fpname, lineno, line) MissingSectionHeaderError: File contains no section headers. file: '<???>', line: 1 'E'
(Jul-13-2020, 04:22 PM)Gribouillis Wrote: [ -> ]A configparser file normally needs a section header
Output:
[mysection] foo = bar
#after added your section but still same issue.
[mysection]
foo = bar
[rootdirectory]
root_directory = e:\alnafi
[username]
user_name = m.bilal
[databaseconnected]
database_connected = True
[summaryunit]
summary_unit = False
Error:
MissingSectionHeaderError: File contains no section headers. file: '<string>', line: 1 'E:\\My Folders\\Alnafi\\AlNafi\\configparser_Ex3.txt'
You are using a wrong signature for ConfigParser.read_file(). This method expects an iterable of strings such as an open file. Replace config.read_file() with config.read().
(Jul-13-2020, 07:36 PM)Gribouillis Wrote: [ -> ]You are using a wrong signature for ConfigParser.read_file(). This method expects an iterable of strings such as an open file. Replace config.read_file() with config.read().

Thanks your guidance, now is working fine...