Obviously it's part of a big application but that's the minimal reproducible example The code should save something in config.ini but I can't manage to make it work when it goes into the whole app:
That's the working code:
import configparser
config = configparser.ConfigParser()
# Add the structure to the file we will create
config.add_section('pos')
config.set('pos', 'x', '0')
config.set('pos', 'y', '0')
# Write the new structure to the new file
with open(r"C:/Users/gberg/Desktop/main/coding/config.ini", 'w') as configfile:
config.write(configfile)
But this doesn't work:
from webbrowser import *
import configparser
config = configparser.ConfigParser()
# Add the structure to the file we will create
config.add_section('pos')
config.set('pos', 'x', '0')
config.set('pos', 'y', '0')
# Write the new structure to the new file
with open(r"C:/Users/gberg/Desktop/main/coding/config.ini", 'w') as configfile:
config.write(configfile)
The error i get:
Traceback (most recent call last):
File "c:\Users\gberg\Desktop\main\coding\frog_prova.py", line 19, in <module>
with open(r"C:/Users/gberg/Desktop/main/coding/config.ini", 'w') as configfile:
TypeError: 'bool' object does not support the context manager protocol
I tried different ways of using configparser but I always get the same error.
Even using
config.write(open(r"C:/Users/gberg/Desktop/main/coding/config.ini", 'w'))
instead of
with open(r"C:/Users/gberg/Desktop/main/coding/config.ini", 'w') as configfile:
config.write(configfile)
still causes problems. Please help me
You have overridden built-in open
with webbrowser.open
:
>>> open('/tmp/fname', 'w')
<_io.TextIOWrapper name='/tmp/fname' mode='w' encoding='UTF-8'>
>>> from webbrowser import *
>>> open('/tmp/fname', 'w')
True
Solution: avoid asterisk imports (from X import *
) like plague.