Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!nntp.giganews.com!usenetcore.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: 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; 'except:': 0.07; 'filename': 0.07; 'behave': 0.09; 'collections': 0.09; 'csv': 0.09; 'executes': 0.09; 'get.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'utilizing': 0.09; 'python': 0.10; 'jan': 0.11; 'exception': 0.13; 'interpreter': 0.15; "'break'": 0.16; 'conditional': 0.16; 'ordereddict': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:continue': 0.16; 'subject:pass': 0.16; 'subject:writing': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'shell': 0.18; 'pass': 0.22; 'am,': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'handling': 0.27; 'print': 0.30; 'run': 0.33; 'jump': 0.33; 'raised': 0.33; '(for': 0.34; 'editor': 0.34; 'except': 0.34; 'false': 0.35; '(and': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:z 10': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'questions': 0.40; 'back': 0.62; 'here': 0.66; 'subject:. ': 0.67; 'uncertain': 0.84; 'subject:this': 0.85; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: continue vs. pass in this IO reading and writing Date: Thu, 3 Sep 2015 12:05:28 -0400 References: <19ca6361-95fe-4a5d-84d6-c72d7941745c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <19ca6361-95fe-4a5d-84d6-c72d7941745c@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441296357 news.xs4all.nl 23742 [2001:888:2000:d::a6]:52995 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95952 On 9/3/2015 11:05 AM, kbtyo wrote: > I am experimenting with many exception handling and utilizing continue vs pass. 'pass' is a do-nothing place holder. 'continue' and 'break' are jump statements [snip] > 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 > 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? Try it for yourself. Copy the following into a python shell or editor (and run) see what you get. for i in [-1, 0, 1]: try: j = 2//i except ZeroDivisionError: print('infinity') continue else: print(j) Change 'continue' to 'pass' and run again. -- Terry Jan Reedy