Closing file opened by ConfigParser

后端 未结 4 1221
北恋
北恋 2020-12-31 08:35

I have the following:

config = ConfigParser()
config.read(\'connections.cfg\')
sections = config.sections()

How can I close the file opened

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 09:31

    Use readfp instead of read:

    with open('connections.cfg') as fp:
        config = ConfigParser()
        config.readfp(fp)
        sections = config.sections()
    

提交回复
热议问题