views:

595

answers:

3

I have this python code for opening a .cfg file, writing to it and saving it:

import ConfigParser 

    def get_lock_file():
        cf = ConfigParser.ConfigParser()
        cf.read("svn.lock")
        return cf
    def save_lock_file(configurationParser):
        cf = configurationParser
        config_file = open('svn.lock', 'w')
        cf.write(config_file)
        config_file.close()

Does this seem normal or am I missing something about how to open-write-save files? Is there a more standard way to read and write config files?

I ask because I have two methods that seem to do the same thing, they get the config file handle ('cf') call cf.set('blah', 'foo' bar) then use the save_lock_file(cf) call above. For one method it works and for the other method the write never takes place, unsure why at this point.

def used_like_this():
        cf = get_lock_file()
        cf.set('some_prop_section', 'some_prop', 'some_value')
        save_lock_file(cf)
+1  A: 

Looks good to me.

If both places call get_lock_file, then cf.set(...), and then save_lock_file, and no exceptions are raised, this should work.

If you have different threads or processes accessing the same file you could have a race condition:

  1. thread/process A reads the file
  2. thread/process B reads the file
  3. thread/process A updates the file
  4. thread/process B updates the file

Now the file only contains B's updates, not A's.

Also, for safe file writing, don't forget the with statement (Python 2.5 and up), it'll save you a try/finally (which you should be using if you're not using with). From ConfigParser's docs:

with open('example.cfg', 'wb') as configfile:
  config.write(configfile)
orip
ok nice. Added that.
perrierism
+1  A: 

Works for me.

C:\temp>type svn.lock
[some_prop_section]
Hello=World

C:\temp>python
ActivePython 2.6.2.2 (ActiveState Software Inc.) based on
Python 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ConfigParser
>>> def get_lock_file():
...         cf = ConfigParser.ConfigParser()
...         cf.read("svn.lock")
...         return cf
...
>>> def save_lock_file(configurationParser):
...         cf = configurationParser
...         config_file = open('svn.lock', 'w')
...         cf.write(config_file)
...         config_file.close()
...
>>> def used_like_this():
...         cf = get_lock_file()
...         cf.set('some_prop_section', 'some_prop', 'some_value')
...         save_lock_file(cf)
...
>>> used_like_this()
>>> ^Z


C:\temp>type svn.lock
[some_prop_section]
hello = World
some_prop = some_value


C:\temp>
Vinay Sajip
Ok good. So I'm not crazy. It doesn't look like a problem with the way I do IO, must be something else the method. Thank you for plugging this in. I'd upvote but need 15 rep
perrierism
+4  A: 

Just to note that configuration file handling is simpler with ConfigObj.

To read and then write a config file:

from configobj import ConfigObj
config = ConfigObj(filename)

value = config['entry']
config['entry'] = newvalue
config.write()
fuzzyman
Ah, really? If I use this I don't have to worry about opening and closing the file, etc?
perrierism
Read the docs. I see. sweet.
perrierism
Nope - ConfigObj handles opening and closing the file for you as necessary.
fuzzyman