f.close should only be called if f is opened.