Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.087 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.02; 'else:': 0.03; 'subject:code': 0.07; 'subject:file': 0.07; "subject:, '": 0.09; "empty')": 0.16; 'received:172.18.0': 0.16; 'subject: \n ': 0.16; '(you': 0.16; 'to:name:python-list@python.org': 0.22; 'header': 0.24; 'header:In-Reply-To:1': 0.27; 'header,': 0.31; 'file': 0.32; 'open': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; 'but': 0.35; 'acceptable': 0.36; 'charset:us-ascii': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'subject:new': 0.38; 'conditions.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'received:unknown': 0.61; 'new': 0.61; 'more': 0.64; 'subject:Using': 0.84; 'victor': 0.84; 'opens': 0.91; 'recover': 0.91; 'race': 0.95 X-Cloudmark-SP-Filtered: true X-Cloudmark-SP-Result: v=1.1 cv=/24PZDiXryEUsaYH72AX4ulVzqSXihAXfE7Ql5lDP0I= c=1 sm=1 a=AgG5ixNBvo4A:10 a=XJDAhjgDJnYA:10 a=7PYXob_7ZXMA:10 a=BLceEmwcHowA:10 a=kj9zAlcOel0A:10 a=xqWC_Br6kY4A:10 a=oNw28mxuUhXRB3mVwYQ4Ag==:17 a=JqIvuYyFbaxO_wY56z4A:9 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 From: "Joseph L. Casale" To: "python-list@python.org" Subject: RE: Using "with open(filename, 'ab'):" and calling code only if the file is new? Thread-Topic: Using "with open(filename, 'ab'):" and calling code only if the file is new? Thread-Index: AQHO1Q0NHudcB+MlkkSnceEwY5yRgpoMeBnwgAAOUBOAAATJ8A== Date: Wed, 30 Oct 2013 02:55:53 +0000 References: <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [172.18.0.201] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383101757 news.xs4all.nl 15863 [2001:888:2000:d::a6]:46838 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57997 > Like Victor says, that opens him up to race conditions. Slim chance, it's no more possible than it happening in the time try/except takes to recover an alternative procedure. with open('in_file') as in_file, open('out_file', 'ab') as outfile_file: if os.path.getsize('out_file'): print('file not empty') else: #write header print('file was empty') And if that's still not acceptable (you did say new) than open the out_file= 'r+' an seek and read to check for a header. But if your file is not new and lacks a header, then what? jlc