How to safely maintain a status file

Discussion in 'Python' started by Richard Baron Penman, Jul 8, 2012.

  1. Hello,

    I want my script to generate a ~1KB status file several times a second.
    The script may be terminated at any time but the status file must not
    be corrupted.
    When the script is started next time the status file will be read to
    check what needs to be done.

    My initial solution was a thread that writes status to a tmp file
    first and then renames:

    open(tmp_file, 'w').write(status)
    os.rename(tmp_file, status_file)

    This works well on Linux but Windows raises an error when status_file
    already exists.
    http://docs.python.org/library/os.html#os.rename


    I guess I could delete the status file:

    open(tmp_file, 'w').write(status)
    if os.path.exists(status_file):
    os.remove(status_file)
    os.rename(tmp_file, status_file)

    and then on startup read from tmp_file if status_file does not exist.
    But this seems awkward.


    Is there a better way? Or do I need to use a database?

    Richard
     
    Richard Baron Penman, Jul 8, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis Lee Bieber

    Re: How to safely maintain a status file

    Dennis Lee Bieber, Jul 8, 2012, in forum: Python
    Replies:
    1
    Views:
    196
    Plumo
    Jul 9, 2012
  2. Laszlo Nagy

    Re: How to safely maintain a status file

    Laszlo Nagy, Jul 8, 2012, in forum: Python
    Replies:
    1
    Views:
    244
    Nobody
    Jul 9, 2012
  3. Plumo
    Replies:
    1
    Views:
    224
    Michael Hrivnak
    Jul 9, 2012
  4. Plumo
    Replies:
    1
    Views:
    221
    Laszlo Nagy
    Jul 12, 2012
  5. John Nagle
    Replies:
    2
    Views:
    328
    Laszlo Nagy
    Jul 12, 2012
Loading...

Share This Page