0

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

2 Answers2

3

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.

Simon Callan
  • 3,020
  • 1
  • 23
  • 34
2

You are writing the same entry over and over againg, use "a" instead of "w" to append the file:

with open("test.txt", "a") as myfile:
    myfile.write("appended text")

Mybe you want to something like this, so the section gets printed once, and you can add multiple items to it:

config.add_section('Section1')
config.set('Section1', 'an_int', '15')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'wb') as configfile:
    config.write(configfile)

Output:

[Section1]
bar = Python
baz = fun
a_bool = true
an_int = 15
foo = %(bar)s is %(baz)s!
a_float = 3.1415

As you see, you should then do it in one write, without calling your function multiple times.

root
  • 76,608
  • 25
  • 108
  • 120
  • 2
    If opened in Append mode, then the config gets written to the file 4 times... not just the Item, but also 4 different sections. I don't believe that is the correct solution. – Alexi Kostibas Oct 11 '12 at 06:26
  • if you use write_stuff() 4 times, then it's bound to get written 4 times... what are you trying to achieve? Edit your question with the outcome you want, at the moment it is hard to understand. – root Oct 11 '12 at 06:28