Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #25042

How to safely maintain a status file

From Richard Baron Penman <richardbp@gmail.com>
Date 2012-07-08 21:29 +1000
Subject How to safely maintain a status file
Newsgroups comp.lang.python
Message-ID <mailman.1916.1341747003.4697.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to safely maintain a status file Richard Baron Penman <richardbp@gmail.com> - 2012-07-08 21:29 +1000
  Re: How to safely maintain a status file Duncan Booth <duncan.booth@invalid.invalid> - 2012-07-09 14:02 +0000

csiph-web