Sharing: File Reader Generator with & w/o Policy

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Mar 15 21:32:10 EDT 2014


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





More information about the Python-list mailing list