Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'mrab': 0.05; 'filename': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'def': 0.12; 'language.': 0.14; 'doing,': 0.16; 'iterable': 0.16; 'length,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'shorten': 0.16; 'subject:File': 0.16; 'language': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'header :User-Agent:1': 0.23; '---': 0.24; 'subject:/': 0.26; '(for': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'errors': 0.30; 'file': 0.32; 'url:python': 0.33; 'reader': 0.33; 'subject:with': 0.35; 'except': 0.35; 'thanks': 0.36; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'free': 0.61; 'length': 0.61; 'subject: ': 0.61; 'url:3': 0.61; 'viruses': 0.61; 'protection': 0.63; 'our': 0.64; 'occur': 0.65; 'here': 0.66; 'close': 0.67; 'antivirus': 0.68; 'subject: & ': 0.68 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Sharing: File Reader Generator with & w/o Policy Date: Sun, 16 Mar 2014 01:32:10 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-22-16.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: X-Antivirus: avast! (VPS 140315-1, 15/03/2014), Outbound message X-Antivirus-Status: Clean 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394933542 news.xs4all.nl 2857 [2001:888:2000:d::a6]:41795 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68375 On 16/03/2014 01:06, Mark H Harris wrote: > On 3/15/14 4:56 PM, MRAB wrote: > >> >> You can also shorten it somewhat: > > Thanks, I like it... I shortened the fnName() also: > > #--------------------------------------------------------- > # fn2Name(filename) generator: file reader iterable > #--------------------------------------------------------- > def fn2Name(filename): > try: > with open(filename, 'r') as fh: <=========== can you tell me > line_count = 0 > for linein in fh: > lineout = linein.strip('\n') > length = len(lineout) > yield((line_count, length, lineout)) > line_count+=1 > except FileNotFoundError as err_code: > print(err_code) > > #--------------------------------------------------------- > > > ... where I can go to find out (for specific contexts) what the > __init__() and __exit__() are actually doing, like for instance in this > case does the filename get closed in __exit__(), and also if errors > occur does the file close automatically? thanks > > marcus Start here http://docs.python.org/3/library/stdtypes.html#context-manager-types -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com