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


Groups > comp.lang.python > #95942

Re: continue vs. pass in this IO reading and writing

Path csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'context': 0.05; 'discard': 0.05; 'except:': 0.07; 'filename': 0.07; 'rest,': 0.07; 'cc:addr:python-list': 0.09; 'behave': 0.09; 'collections': 0.09; 'csv': 0.09; 'exception,': 0.09; 'executes': 0.09; 'functional.': 0.09; 'exception': 0.13; 'interpreter': 0.15; 'conditional': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'headers,': 0.16; 'iteration': 0.16; 'ordereddict': 0.16; 'received:mail-ig0-x22a.google.com': 0.16; 'subject:continue': 0.16; 'subject:pass': 0.16; 'subject:writing': 0.16; 'wrote:': 0.16; 'try:': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'assumes': 0.22; 'body,': 0.22; 'sep': 0.22; 'pass': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header': 0.24; '(which': 0.26; 'rest': 0.26; 'compatible': 0.27; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; '"do': 0.29; 'loop,': 0.29; 'print': 0.30; 'code': 0.30; 'statement': 0.32; 'raised': 0.33; '(for': 0.34; 'case,': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'false': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; '(and': 0.36; 'beginning': 0.36; 'subject:: ': 0.37; 'files': 0.38; 'means': 0.39; 'does': 0.39; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'body': 0.61; 'further': 0.62; 'back': 0.62; 'here': 0.66; 'subject:. ': 0.67; 'chrisa': 0.84; 'presumably': 0.84; 'uncertain': 0.84; 'subject:this': 0.85; 'to:none': 0.91; 'safe.': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=jIGHt5hyte9sQlXpeAVrjDKHGrFKO2UMIsIel9h0ivs=; b=igfv+jgtKP5jznknjxX0hmRliJC+gge1Xw8e9dh+AnTKbOKQstuWl0g/AmwYc290ke /ywYWGY5wlxU33bWzPzHaI7S7m8aetglRAaP5CF5UXsUEu46ZYmLlcO49e/CEg6aFriJ uG3iqsL5qC1ln0Or5Buq+gaIf4hA1vx+Ft6MGlSB1v/oEo0aRa9K7gFIvcD6qReL+k6E EHBRF1SLAhFADj+1UO0ADCXNo2SWfuiUYdrUl8r7C0vHDKp6nUFzuxZ1kqO65uCiEGCV epYspQZOPAJ55VKbPRvrYhp38SvH7WpoBL+TzbcrpMJorlLNtBPUL3oPvBH0Xf30CcbP 7Y5w==
MIME-Version 1.0
X-Received by 10.50.114.100 with SMTP id jf4mr14925885igb.94.1441294033308; Thu, 03 Sep 2015 08:27:13 -0700 (PDT)
In-Reply-To <19ca6361-95fe-4a5d-84d6-c72d7941745c@googlegroups.com>
References <19ca6361-95fe-4a5d-84d6-c72d7941745c@googlegroups.com>
Date Fri, 4 Sep 2015 01:27:13 +1000
Subject Re: continue vs. pass in this IO reading and writing
From Chris Angelico <rosuav@gmail.com>
Cc "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.20+
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.70.1441294042.8327.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1441294042 news.xs4all.nl 23741 [2001:888:2000:d::a6]:40293
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95942

Show key headers only | View raw


On Fri, Sep 4, 2015 at 1:05 AM, kbtyo <ahlusar.ahluwalia@gmail.com> wrote:
> However, I am uncertain as to how this executes in a context like this:
>
> import glob
> import csv
> from collections import OrderedDict
>
> interesting_files = glob.glob("*.csv")
>
> header_saved = False
> with open('merged_output_mod.csv','w') as fout:
>
>     for filename in interesting_files:
>         print("execution here again")
>         with open(filename) as fin:
>             try:
>                 header = next(fin)
>                 print("Entering Try and Except")
>             except:
>                 StopIteration
>                 continue

I think what you want here is:

except StopIteration:
    continue

The code you have will catch _any_ exception, and then look up the
name StopIteration (and discard it).

>             else:
>                 if not header_saved:
>                     fout.write(header)
>                     header_saved = True
>                     print("We got here")
>                 for line in fin:
>                     fout.write(line)
>
> My questions are (for some reason my interpreter does not print out any readout):
>
> 1. after the exception is raised does the continue return back up to the beginning of the for loop (and the "else" conditional is not even encountered)?
>
> 2. How would a pass behave in this situation?

The continue statement means "skip the rest of this loop's body and go
to the next iteration of the loop, if there is one". In this case,
there's no further body, so it's going to be the same as "pass" (which
means "do nothing").

For the rest, I think your code should be broadly functional. Of
course, it assumes that your files all have compatible headers, but
presumably you know that that's safe.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

continue vs. pass in this IO reading and writing kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-09-03 08:05 -0700
  Re: continue vs. pass in this IO reading and writing Chris Angelico <rosuav@gmail.com> - 2015-09-04 01:27 +1000
    Re: continue vs. pass in this IO reading and writing kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-09-03 08:38 -0700
      Re: continue vs. pass in this IO reading and writing Chris Angelico <rosuav@gmail.com> - 2015-09-04 01:51 +1000
        Re: continue vs. pass in this IO reading and writing kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-09-03 08:57 -0700
          Re: continue vs. pass in this IO reading and writing Chris Angelico <rosuav@gmail.com> - 2015-09-04 02:11 +1000
            Re: continue vs. pass in this IO reading and writing kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-09-03 09:35 -0700
    Re: continue vs. pass in this IO reading and writing kbtyo <ahlusar.ahluwalia@gmail.com> - 2015-09-03 08:49 -0700
  Re: continue vs. pass in this IO reading and writing Terry Reedy <tjreedy@udel.edu> - 2015-09-03 12:05 -0400
  Re: continue vs. pass in this IO reading and writing Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-09-03 18:37 +0200

csiph-web