Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'startup': 0.05; 'raises': 0.07; 'subject:file': 0.07; 'terminated': 0.07; 'tmp': 0.07; 'subject:How': 0.09; 'exist.': 0.09; 'second.': 0.09; 'thread': 0.11; 'tmp_file': 0.16; 'windows': 0.19; 'exists.': 0.22; 'file:': 0.22; 'seems': 0.23; 'linux': 0.24; 'script': 0.24; 'done.': 0.27; 'guess': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'initial': 0.28; 'way?': 0.29; "skip:' 10": 0.30; 'writes': 0.30; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'richard': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'does': 0.37; 'received:209': 0.37; 'skip:o 20': 0.38; 'url:docs': 0.38; 'delete': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; 'first': 0.61; 'times': 0.63; 'subject:status': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=4if+kdYAeXEf3kAVBPPCfd5cGXs9P4DzkhaLb4YEst0=; b=0e2za9rH92dkHty+QXahMKarW3L2LgzL4BEyeLI7IXoRh6dnEjVtYWIw7y/1voBadC pQ91lUWQ4LXtlEqYTCr3wIESwatiXXnbXcFys/RzxxfCV+FrcT2QzhhjogRJjQmFnkYM dUcRC59i3qHOTp8VTFXysPp8L9ihdZ97ZbND4HOK2SZTULn021FGhU1gD83CcOWUdkE6 jbiuSN1ra3Ezj7z4UxTz+fd2x7Vghp7x819w30JI8LF3yRRd9G4+uDEVa0qmtEP41g0/ B/VTNCIGT9lQVHTMdVDUp4HIsOC7XcOYSLQWH35it5Nj3MuT4k4H7Ab/0a3Ymu5y2jJF sReQ== MIME-Version: 1.0 From: Richard Baron Penman Date: Sun, 8 Jul 2012 21:29:41 +1000 Subject: How to safely maintain a status file To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1341747003 news.xs4all.nl 6881 [2001:888:2000:d::a6]:60917 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25042 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