I'm attempting to port a Python2.7 script to Python3.6+ and have hit a roadblock my google searching couldn't resolve. The issue is that a try: except: call below doesn't appear to work after doing some initial porting suggestions. I'm sure this is something simple; just escapes me at the moment.
Python2.7 code: (worked)
import ConfigParser
logOutCfg = ConfigParser.ConfigParser()
try:
if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except ConfigParser.MissingSectionHeaderError as e:
pass
except ConfigParser.ParsingError as e:
print(str(e))
pass
Python3.6 attempted code (doesn't work under Python 2.7):
from configparser import ConfigParser
logOutCfg = ConfigParser()
try:
if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except ConfigParser.MissingSectionHeaderError as e:
pass
except ConfigParser.ParsingError as e:
print(str(e))
pass
The error reported by the script when running under Python2 is:
File "<script>.py", line 242, in <function>
except ConfigParser.MissingSectionHeaderError:
AttributeError: type object 'ConfigParser' has no attribute 'MissingSectionHeaderError'
I'm pretty sure I tried a bunch of different things; including: except configparser.MissingSectionHeaderError
but with no joy.
What am I missing? I need the code to work in both Python2 and Python3 for the foreseeable future... at least the next 9 months.
@mkrieger1 was right on the money. The key was to also import configparser
import configparser
from configparser import ConfigParser
logOutCfg = ConfigParser()
try:
if (os.path.isfile(logOutfilename)) : logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
pass
except configparser.ParsingError as e:
print(str(e))
pass
Your Python 3.6 code is mostly correct. The only problem is that you are trying to grab the exceptions from the ConfigParser
class instead of the configparser
module. The main confusion here is most likely coming from the fact that name of the module changed in Python 3 to be more inline with PEP8 standards . Originally, the ConfigParser
module and class shared the same name, but this is not the case in Python 3.
The below code should work in Python 2 and 3.
try:
import configparser
except ImportError:
import ConfigParser as configparser
logOutCfg = configparser.ConfigParser()
try:
if (os.path.isfile(logOutfilename)):
logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
pass
except configparser.ParsingError as e:
print(e)
Here is a quick breakdown of what is going on here.
try:
import configparser
except ImportError:
import ConfigParser as configparser
Try to import the Python 3 configparser
module. If in Python 2, this will raise an ImportError
. We catch that, and instead import ConfigParser
and alias it with configparser
.
logOutCfg = configparser.ConfigParser()
Grab the ConfigParser
class from the configparser
module imported above, and instantiate it.
try:
if (os.path.isfile(logOutfilename)):
logOutCfg.read(logOutfilename)
except configparser.MissingSectionHeaderError as e:
pass
except configparser.ParsingError as e:
print(e)
Try to open the file as you were before, but instead of attempting to get the exceptions from the ConfigParser
class, get them from the configparser
module.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.