Use with statement rather than try: finaly: x.close().