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?

From "Joseph L. Casale" <jcasale@activenetwerx.com>
Subject RE: Using "with open(filename, 'ab'):" and calling code only if the file is new?
Date 2013-10-30 02:55 +0000
References <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> <c934700beee44f7a8c14de2403c42bac@exch.activenetwerx.com> <l4ppvg$sfn$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.1788.1383101757.18130.python-list@python.org> (permalink)

Show all headers | 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