Python Forum
configparser module, when use text file to show following error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
configparser module, when use text file to show following error
#1
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'
-----------
Reply
#2
A configparser file normally needs a section header
Output:
[mysection] foo = bar
Reply
#3
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'
Reply
#4
(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'
Reply
#5
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().
Reply
#6
(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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert text from an image to a text file Evil_Patrick 5 4,303 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,456 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Login Module Help - Comparing data in a text file to data held in a variable KeziaKar 0 2,259 Mar-08-2018, 11:41 AM
Last Post: KeziaKar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020