close()關(guān)閉文件流
當(dāng)調(diào)用close()方法時(shí),首先會(huì)把緩沖區(qū)數(shù)據(jù)寫(xiě)入文件(也可以直接調(diào)用flush()方法),在關(guān)閉文件,釋放文件對(duì)象
try:
f=open(r'my01.txt','w')
str='gaoji'
strs=['hello\n','world\n']
f.write(str) #文件寫(xiě)入緩沖區(qū)
f.writelines(strs) #文件寫(xiě)入緩沖區(qū)
except BaseException as e:
print(e)
finally:
f.close() #有沒(méi)有異常都會(huì)執(zhí)行finally