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