How to write a memory efficient Python program?,f.close()