how to write a memory efficient python program?,f.close()