Change value in ini file using ConfigParser Python

后端 未结 3 1452
我在风中等你
我在风中等你 2021-02-05 14:20

So, I have this settings.ini :

[SETTINGS]

value = 1

And this python script

from ConfigParser import SafeConfigParser

parser =         


        
3条回答
  •  -上瘾入骨i
    2021-02-05 15:01

    I had an issue with:with open

    Other way:

    import configparser
    
    def set_value_in_property_file(file_path, section, key, value):
        config = configparser.RawConfigParser()
        config.read(file_path)
        config.set(section,key,value)                         
        cfgfile = open(file_path,'w')
        config.write(cfgfile, space_around_delimiters=False)  # use flag in case case you need to avoid white space.
        cfgfile.close()
    

    It can be used for modifying java properties file: file.properties

提交回复
热议问题