赞
踩
文件读写:
open(‘ xxx ')
open(‘ xxx’,'w')
.read()
.write()
.close()
最后一定要关闭文件,不然可能会有问题,存在数据丢失
法1:
with open('a.txt','w') as f:
f.write('abc')
法2:txt = open('a.txt','w')
try:
txt.write('abc'+'\n')
except Exception:
print('error')
finally:
txt.close()
txt = open('./data/a.txt')
txt_read = txt.read()
print(txt_read)
txt.close()
txt = open('a.txt')txt.close()
txt = open('a.txt','w') ##覆盖
txt.write('jintian qianqi bucuo \n')
txt.write('hhhhhhhhhhhhhhhhhhhh \n')
txt.close()
txt = open('a.txt')
txt_read = txt.read()
print(txt_read)
txt.close()
txt = open('a.txt','a') ##追加
txt.write('jintian qianqi bucuo \n')
txt.write('hhhhhhhhhhhhhhhhhhhh \n')
txt.close()
txt = open('a.txt')
txt_read = txt.read()
print(txt_read)
txt.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。