Sharing: File Reader Generator with & w/o Policy
Mark H Harris
harrismh777 at gmail.com
Sat Mar 15 21:06:15 EDT 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):
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
More information about the Python-list
mailing list