ConfigParser: NoSectionError when a get is after a set on TextCtrl

Hi,

I’ve a config.set that messes all the config.get that come after. The error I get in the example below is:

configparser.NoSectionError: No section: ‘Margin’

This is the config.set that causes the problem:

cbTb = wx.CheckBox(panel, label='Tb')
cbTb.SetFont(textfont)
self.Bind(wx.EVT_CHECKBOX, self.taboolaStatus, cbTb)
cbTb.SetValue(1)
**#This is where it starts**
config = configparser.ConfigParser()
config.read(self.configFile)
cfgfile = open(self.configFile, 'w')
config.set('Network', 'tb', "true")
config.write(cfgfile)

And this is where I get the error:

marginLabel = wx.StaticText(panel, wx.ID_ANY, 'Margin (%): ')
marginLabel.SetFont(textfont)
**#The error is flagged here** marginDefault = config.get('Margin', 'value')
marginInput = wx.TextCtrl(panel, wx.ID_ANY, size=(45, 24))
marginInput.SetFont(font)
config = configparser.ConfigParser()
config.read(self.configFile)
**marginDefault = config.get('Margin', 'value')**
marginInput.SetValue(marginDefault)
marginInput.SetMaxLength(2)

If I remove the part after “#This is where it starts” the script runs smooth. What is causing this weird behavior?

Thank you,

Barb-Cr33k wrote:

I've a config.set that messes all the config.get that come after. The error I get in the example below is:

configparser.NoSectionError: No section: 'Margin'

This is the config.set that causes the problem:

...

>*#This is where it starts*||config = configparser.ConfigParser() config.read(self.configFile) cfgfile = open(self.configFile, 'w') config.set('Network', 'tb', "true") config.write(cfgfile)|

When you open the file for writing, that erases the contents of the file. "config" writes the configuration to it, but your file handle is still open, so nothing gets flushed to disk. Thus, the file on disk is empty.

I suggest you try this:

 config = configparser\.ConfigParser\(\)
 config\.read\(self\.configFile\)
 config\.set\('Network', 'tb', 'true'\)
 config\.write\(open\(self\.configFile, 'w'\)\)

That way, the file will be closed and flushed immediately after the write.

By the way, you didn't show us the contents of the config file. Is there actually a [Margin] section in the file?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thank you it worked. Silly mistake indeed.

···

On Thursday, 30 May 2019 13:23:24 UTC-4, Tim Roberts wrote:

Barb-Cr33k wrote:

        I've a config.set that messes all

the config.get that come after. The error I get in the
example below is:

        configparser.NoSectionError: No

section: ‘Margin’

        This is the config.set that causes

the problem:

**#This is where it starts**
config = configparser.ConfigParser()
config.read(self.configFile)
cfgfile = open(self.configFile, 'w')
config.set('Network', 'tb', "true")
config.write(cfgfile)
  When you open the file for writing, that erases the contents of

the file. “config” writes the configuration to it, but your file
handle is still open, so nothing gets flushed to disk. Thus, the
file on disk is empty.

I suggest you try this:

config = configparser.ConfigParser()
config.read(self.configFile)
config.set(‘Network’, ‘tb’, ‘true’)
config.write(open(self.configFile, ‘w’))

  That way, the file will be closed and flushed immediately after

the write.

  By the way, you didn't show us the contents of the config file. 

Is there actually a [Margin] section in the file?

-- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.