Instead of the fid = open..., fid.close... I'd prefer a "with file(...) as fid", which is robust to exceptions and more modern style.