Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8684285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:15:58+00:00 2026-06-12T22:15:58+00:00

Given the following script: import ConfigParser from datetime import datetime import time def write_stuff():

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:15:59+00:00Added an answer on June 12, 2026 at 10:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script: import os import stat curDir = os.getcwd()+'/test' for (paths,
Given the following python script.... $ cat readStdin.py #!/usr/bin/python import sys var = .join(sys.stdin.readlines()).rstrip()
I have the following script which uses SocksiPY and Tor: from TorCtl import TorCtl
Given the following html table and script shown below I am having a problem
I want to Make selenium script which move slider given on following site Example
Given following Statment: String query = "Select * from T_spareParts where SparePartPK IN (?
I have a simple IronPython script: # Foo.py import os def main(): print( Hello
I use the latest version of numpy/scipy. The following script does not work: import
I have the following script that is taken from a python game development book.
I am trying to run the following HelloWorld Script at Command Line import tornado.httpserver

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.