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


Groups > comp.lang.python > #68375

Re: Sharing: File Reader Generator with & w/o Policy

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Sharing: File Reader Generator with & w/o Policy
Date 2014-03-16 01:32 +0000
References <lg2h87$6fj$1@speranza.aioe.org> <mailman.8156.1394920773.18130.python-list@python.org> <lg2te7$oq$1@speranza.aioe.org>
Newsgroups comp.lang.python
Message-ID <mailman.8157.1394933542.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 16:38 -0500
  Re: Sharing: File Reader Generator  with & w/o Policy MRAB <python@mrabarnett.plus.com> - 2014-03-15 21:56 +0000
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 19:36 -0500
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 19:45 -0500
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 20:06 -0500
      Re: Sharing: File Reader Generator  with & w/o Policy Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-16 01:32 +0000
        Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 21:52 -0500
  Re: Sharing: File Reader Generator  with & w/o Policy Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-16 02:01 +0000
    Re: Sharing: File Reader Generator  with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 22:34 -0500
      Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 14:48 +1100
        Re: Sharing: File Reader Generator with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-15 23:47 -0500
          Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 16:41 +1100
            Re: Sharing: File Reader Generator with & w/o Policy Mark H Harris <harrismh777@gmail.com> - 2014-03-16 01:19 -0500
              Re: Sharing: File Reader Generator with & w/o Policy Chris Angelico <rosuav@gmail.com> - 2014-03-16 17:37 +1100
          Re: Sharing: File Reader Generator with & w/o Policy Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-16 10:36 +0000

csiph-web