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?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <zachary.ware@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.051
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'subject:code': 0.07; 'subject:file': 0.07; 'input,': 0.09; "subject:, '": 0.09; 'csv': 0.16; 'guessing': 0.16; 'ioerror,': 0.16; 'subject: \n ': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'to:name:python- list@python.org': 0.22; 'exists': 0.24; 'cheers,': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'file:': 0.31; 'file': 0.32; 'another': 0.32; 'open': 0.33; 'not.': 0.33; 'subject:the': 0.34; 'subject:with': 0.35; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'list': 0.37; 'skip:o 20': 0.38; 'subject:new': 0.38; 'conditions.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'subject:Fwd': 0.61; 'first': 0.61; 'subject:Using': 0.84; 'victor': 0.84; 'opens': 0.91; 'race': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=foheBuCldjLpNa8db2OFoCmaleBALKDbzYjAxxo73ok=; b=XqlM1p/61VLFY/93z3psmH2/626CxvNZojGPVEtLkadhOlWoknVvW00BPv1RiYeGDt /4IgBEtLz7yZJHCiLX+GyAESTbI14J8BCONV3pNt4UcfVIndsQE+3XZJav71b5mzxV7n ArWDs0BRBd4nog84+l2mPgVBahrRFaMNymfKSVOfMXFFkda8/uK9ssNGjzPKb0Qol8nH ww3FuK8IIf/T228x6Q5fbkIXqjYSTx10FhcknuixAIY6qDGVSQGONKN2RsMzqvaAo4Yl 3LjzWxVJBaLvgv3dhd24xYePhI/n5qR76YtoiXME+sqx6GOkrrJHa/aAz/0Y4sdo58yJ KqBg==
X-Received by 10.204.103.199 with SMTP id l7mr1418228bko.11.1383103741464; Tue, 29 Oct 2013 20:29:01 -0700 (PDT)
MIME-Version 1.0
Sender zachary.ware@gmail.com
In-Reply-To <CAKJDb-PLjRNB=HrxEx-QZanHmiBJSeMkSCsQfCmqY48o2TZxKg@mail.gmail.com>
References <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> <CAKJDb-PLjRNB=HrxEx-QZanHmiBJSeMkSCsQfCmqY48o2TZxKg@mail.gmail.com>
From Zachary Ware <zachary.ware+pylist@gmail.com>
Date Tue, 29 Oct 2013 22:28:41 -0500
X-Google-Sender-Auth j0W6qVoDFwTjutmcYJV6deC9_Bg
Subject Fwd: Using "with open(filename, 'ab'):" and calling code only if the file is new?
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.1789.1383104100.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383104100 news.xs4all.nl 15916 [2001:888:2000:d::a6]:60152
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58000

Show key headers only | 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