Use file.close instead of os.close().