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: 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: References: <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> From: Zachary Ware 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" 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Oct 29, 2013 at 8:02 PM, Victor Hooi 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...)