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


Groups > comp.lang.python > #58000

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

References <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> <CAKJDb-PLjRNB=HrxEx-QZanHmiBJSeMkSCsQfCmqY48o2TZxKg@mail.gmail.com>
From Zachary Ware <zachary.ware+pylist@gmail.com>
Date 2013-10-29 22:28 -0500
Subject Fwd: Using "with open(filename, 'ab'):" and calling code only if the file is new?
Newsgroups comp.lang.python
Message-ID <mailman.1789.1383104100.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Oct 29, 2013 at 8:02 PM, Victor Hooi <victorhooi@gmail.com> wrote:
> Hi,
>
> I have a CSV file that I will repeatedly appending to.
>
> I'm using the following to open the file:
>
>     with open(self.full_path, 'r') as input, open(self.output_csv, 'ab') as output:
>         fieldnames = (...)
>         csv_writer = DictWriter(output, filednames)
>         # Call csv_writer.writeheader() if file is new.
>         csv_writer.writerows(my_dict)
>
> I'm wondering what's the best way of calling writeheader() only if the file is new?
>
> My understanding is that I don't want to use os.path.exist(), since that opens me up to race conditions.
>
> I'm guessing I can't use try-except with IOError, since the open(..., 'ab') will work whether the file exists or not.
>
> Is there another way I can execute code only if the file is new?
>
> Cheers,
> Victor

I've not tested, but you might try

with ... open(...) as output:
    ...
    if output.tell() == 0:
        csv_writer.writeheader()
...

HTH

--
Zach

(failed to send to the list first time around...)

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