Closing file opened by ConfigParser

后端 未结 4 1231
北恋
北恋 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:24

    To test your suspicion, use ConfigParser.readfp() and handle opening and closing of the file by yourself. Make the readfp call after the changes are made.

    config = ConfigParser()
    #...on each change
    fp = open('connections.cfg')
    config.readfp(fp)
    fp.close()
    sections = config.sections()
    

提交回复
热议问题