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


Groups > comp.lang.python > #57994

RE: 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!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.030
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'stops': 0.07; 'subject:code': 0.07; 'subject:file': 0.07; 'input,': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; "subject:, '": 0.09; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'specify': 0.24; 'header': 0.24; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'wondering': 0.29; 'forgot': 0.30; "i'm": 0.30; 'lines': 0.31; 'file': 0.32; 'running': 0.33; 'checking': 0.33; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; 'there': 0.35; 'really': 0.36; 'instances': 0.36; 'module.': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'similar': 0.36; 'should': 0.36; 'so,': 0.37; 'two': 0.37; 'skip:o 20': 0.38; 'subject:new': 0.38; 'problems': 0.38; 'arrange': 0.38; 'conditions.': 0.38; 'to:addr:python-list': 0.38; 'environment.': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'skip:u 10': 0.60; "you'll": 0.62; "you've": 0.63; 'more': 0.64; 'between': 0.67; 'partial': 0.84; 'subject:Using': 0.84; 'victor': 0.84; 'opens': 0.91; 'received:108': 0.93; 'race': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject RE: Using "with open(filename, 'ab'):" and calling code only if the file is new?
Date Wed, 30 Oct 2013 02:13:05 +0000 (UTC)
References <68bd6cb6-44b2-446c-b0e2-043e3ac1c35b@googlegroups.com> <assp.0015a4d561.c934700beee44f7a8c14de2403c42bac@exch.activenetwerx.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-108-12-92-177.hstntx.dsl-w.verizon.net
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
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.1786.1383099209.18130.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383099209 news.xs4all.nl 15952 [2001:888:2000:d::a6]:40216
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:57994

Show key headers only | View raw


On 29/10/2013 21:42, Joseph L. Casale wrote:

You forgot the attribution line:  "Victor says"
>>     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.
>
> What stops you from checking before and setting a flag?

Like Victor says, that opens him up to race conditions.

Victor:

You need to more completely specify your environment.  Are there
multiple instances of this or similar program running simultaneously? 
If so, you've got lots more problems than a missing or duplicated header
line.  You could get partial lines intermixing between the two outputs.

Chances are if you really need to support more than one program at the
same time, you'll need to use a lower-level open, perhaps from the os
module.  Some form of locking is called for.  And if the data SHOULD be
interleaved, you'll have to arrange it so it gets done in whole number
increments.

-- 
DaveA

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