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


Groups > comp.lang.python > #57997

RE: Using "with open(filename, 'ab'):" and calling code only if the file is new?

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 <jcasale@activenetwerx.com>
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" <jcasale@activenetwerx.com>
To "python-list@python.org" <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> <c934700beee44f7a8c14de2403c42bac@exch.activenetwerx.com> <l4ppvg$sfn$1@ger.gmane.org>
In-Reply-To <l4ppvg$sfn$1@ger.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1788.1383101757.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


> 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

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


Thread

Using "with open(filename, 'ab'):" and calling code only if the file is new? Victor Hooi <victorhooi@gmail.com> - 2013-10-29 18:02 -0700
  RE: Using "with open(filename, 'ab'):" and calling code only if the file is new? "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-10-30 01:42 +0000
  RE: Using "with open(filename, 'ab'):" and calling code only if the file is new? Dave Angel <davea@davea.name> - 2013-10-30 02:13 +0000
  RE: Using "with open(filename, 'ab'):" and calling code only if the file is new? "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-10-30 02:55 +0000
    Re: Using "with open(filename, 'ab'):" and calling code only if the file is new? Victor Hooi <victorhooi@gmail.com> - 2013-10-29 20:22 -0700
  Fwd: Using "with open(filename, 'ab'):" and calling code only if the file is new? Zachary Ware <zachary.ware+pylist@gmail.com> - 2013-10-29 22:28 -0500
  Re: Using "with open(filename, 'ab'):" and calling code only if the file is new? Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-10-30 08:53 +0100
  Re: Using "with open(filename, 'ab'):" and calling code only if the file is new? Neil Cerutti <neilc@norwich.edu> - 2013-10-30 13:23 +0000

csiph-web