Update field with ConfigParser -Python-
Solution 1
- open the config file
- read the content using ConfigParser
- close the file
- update the config, in memory now
- open the same file with w+
- write the updated in-memory content to the file
- close the file
Solution 2
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('properties.ini')
dhana = {'key': 'valu11'}
parser.set('CAMPAIGNS', 'zoho_next_campaign_map', str(dhana))
with open("properties.ini", "w+") as configfile:
parser.write(configfile)
Solution 3
Yes, it is normal that set
operates on the information in memory rather than on the file from which the information was originally read.
write
ought to be what you want. How exactly did you use it, what exactly did it do, and how did that differ from what you wanted?
Incidentally, you should generally be using ConfigParser.SafeConfigParser
rather than ConfigParser.ConfigParser
unless there's a specific reason for doing otherwise.
Moving forward with Python 3.x SafeConfigParser
will be merged/renamed as ConfigParser
so SafeConfigParser
will eventually be deprecated and phased out.
Solution 4
I ran into the same issue and figure out that this worked for me:
def update_system_status_values(file, section, system, value):
config.read(file)
cfgfile = open(file, 'w')
config.set(section, system, value)
config.write(cfgfile)
cfgfile.close()
1) Read it
2) Open it
3) Update it
4) Write it
5) Close it
Francisco
Updated on June 09, 2022Comments
-
Francisco almost 2 years
I thought that the set method of ConfigParser module updates the field given, but, it seems that the change remains only in memory and doesn't get into the config file. Is it a normal behaviour?
I have also tried the write method, but what I got was another replicated section which by so far is not what I want.
Here is a specimen which represents what I'm doing:
import sys import ConfigParser if __name__=='__main__': cfg=ConfigParser.ConfigParser() path='./../whatever.cfg/..' c=cfg.read(path) print cfg.get('fan','enabled') cfg.set('fan','enabled','False') c=cfg.read(path) print cfg.get('fan','enabled')
-
Francisco about 13 yearsHere`s how I'm using write: with open(self.path,'a') as configfile: self.cfg.write(configfile) write(self.cfg)
-
Gareth McCaughan about 13 yearsWhy are you appending to the file instead of replacing it?
-
Francisco about 13 yearsBecause of common sense, I think. Why have to rewrite the whole file if you only want to change a single field?
-
Gareth McCaughan about 13 yearsSuppose the single field is defined at the start of the file (or, in fact, anywhere other than the end). Then your choices are: (1) rewrite the file or (2) append to the file and accept the redundancy. Option 2 gets very bad if the config is repeatedly changed, so option 1 it is. If that goes against your common sense, then you might want to consider the possibility that your common sense needs a little adjustment.
-
Francisco about 13 yearsI like your last phrase. LOL.
-
Francisco about 13 yearsAnyway, opt 2 is not an option either. I will go with the first one. But It surprises me a lot that there isn't a, lets say, 'commit or update' method where you can relay your updates of the config file. Low eficiency, I would risk to say.
-
Gareth McCaughan about 13 yearsReally, how often does a config file like this need updating? If it's happening often, the file will be in the OS's cache and rewriting the whole thing will take less than a millisecond on a typical machine. If it's not happening often, who cares about a few ms of extra time? So unless you're updating the config file, say, 100 times per second, I don't see that there's a problem here. (And if you are, you should do something else instead.)
-
Francisco about 13 yearsRight. I see your point. In fact, this update in my config file is a consecuence of a command given by the user. So, pushing to the limit, can five or ten times a day. The overhead is present, but is not critical. If there's no way of update a single value, I will have to rewrite it all like the rest of the world who use ConfigParser
-
humble_wolf almost 4 yearswhy do you think step 3/5 is needed ? I guess this might produce inconsistencies if two processes are writing the same file at almost same time.