OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

File contains no section headers - Python

  • Thread starter Thread starter user3673471
  • Start date Start date
U

user3673471

Guest
I am trying to read a configuration file in python 3.7 using SafeConfigParser. I have tried giving the full file name (with file location), giving only file name (without file location), using readfp function in SafeConfigParser, using only configparser instead of safeconfigparser but none of them have worked. I am 100% sure that at least the correct file is being read.

Here is my python code:

Code:
from configparser import SafeConfigParser
import os

def main():
    filename = "C:/Users/Umer Sherdil Paracha/Desktop/distutils.cfg"
    if os.path.isfile(filename):
        parser = SafeConfigParser()
        parser.read(filename)
        print(parser.sections())
        screen_width = parser.getint('graphics','width')
        screen_height = parser.getint('graphics','height')
    else:
        print("Config file not found")

if __name__=="__main__":
    main()

and here is my cfg file:

Code:
[GRAPHICS]
height = 600
width = 800

I am totally stuck on this stupid problem. Any help in this regard would be appreciated. Thank you.
<p>I am trying to read a configuration file in python 3.7 using SafeConfigParser. I have tried giving the full file name (with file location), giving only file name (without file location), using readfp function in SafeConfigParser, using only configparser instead of safeconfigparser but none of them have worked. I am 100% sure that at least the correct file is being read. </p>

<p>Here is my python code:</p>

<pre><code>from configparser import SafeConfigParser
import os

def main():
filename = "C:/Users/Umer Sherdil Paracha/Desktop/distutils.cfg"
if os.path.isfile(filename):
parser = SafeConfigParser()
parser.read(filename)
print(parser.sections())
screen_width = parser.getint('graphics','width')
screen_height = parser.getint('graphics','height')
else:
print("Config file not found")

if __name__=="__main__":
main()
</code></pre>

<p>and here is my cfg file:</p>

<pre><code>[GRAPHICS]
height = 600
width = 800
</code></pre>

<p>I am totally stuck on this stupid problem. Any help in this regard would be appreciated. Thank you.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top