def iter_and_close(file_like, block_size):
 while 1:
  try:
   block = file_like.read(block_size)
   if block:
    (yield block)
   else:
    raise StopIteration
  except StopIteration as si:
   file_like.close()
   return

def iter_and_close ( file_like , block_size ) : while 1 : try : block = file_like . read ( block_size ) if block : ( yield block ) else : raise StopIteration except StopIteration as si : file_like . close ( ) return
yield file contents by block then close the file .