Search code examples
pythonconfigparser

Why does ConfigParser not write to disk immediately?


Given the following script:

import ConfigParser
from datetime import datetime
import time

def write_stuff():
    section = "test"
    item = "oh hey there"
    conf_filename = "test.conf"

    conf = ConfigParser.ConfigParser()
    conf.readfp(open(conf_filename, 'r', 0))

    timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")

    conf.set(section, timestamp, item)

    with open(conf_filename, "w", 0) as conf_file:
        # time.sleep(1)
        conf.write(conf_file)

write_stuff()
write_stuff()
write_stuff()
write_stuff()

It will only write one entry to the file, as illustrated by:

$ touch test.conf
$ python tests.py  # this is what I've named the above
$ $ cat test.conf
[test]
2012-10-10_231439 = oh hey there

However, if you uncomment the time.sleep(1), all entries appear. Strangely (to me, anyway,) this even happens if you have one call to write_stuff(), and call the script in quick succession from the shell. I would think that once Python exits, whatever is going to disk would have gone to disk. What's going on?

Environment: Python 2.7.3 on Mac OS X 10.8


Solution

  • The problem here is that the key value you are using in the config file is a time stamp with a 1 sec resolution. This means, when you call write_stuff() four times in a row, the time hasn't changed, the time stamp doesn't change, and you simply overwrite the previous value, rather than adding a new value.

    What you need to do is generate a unique key value each time. If you want to keep the timestamp value, something this would work:

    count = 0
    
    def write_stuff():
        global count
    
        section = "test" 
        item = "oh hey there" 
        conf_filename = "test.conf" 
    
        conf = ConfigParser.ConfigParser() 
        conf.readfp(open(conf_filename, 'r', 0)) 
    
        timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S")+ "_%s" % count
        count += 1
    
        conf.set(section, timestamp, item) 
    
        with open(conf_filename, "w", 0) as conf_file: 
            conf.write(conf_file) 
    

    Note, the values written to the config file won't be in any particular order.