Sharing: File Reader Generator with & w/o Policy

Mark H Harris harrismh777 at
Sun Mar 16 02:06:15 CET 2014

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):
         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))
     except FileNotFoundError as 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


More information about the Python-list mailing list