python写文件是否覆盖原来内容

python写文件是否覆盖原来内容

python写文件

python写文件的两种方式:覆盖、不覆盖原来内容

覆盖原来内容

txt = ‘landmark.txt’
wrf = open(txt, ‘w’)
wrf.write(‘test01’ + ‘\n’)
wrf.close()

txt = ‘landmark.txt’
wrf = open(txt, ‘w’)
wrf.write(‘test02’ + ‘\n’)
wrf.close()
结果:
test02

不覆盖原来内容

txt = ‘landmark.txt’
wrf = open(txt, ‘w’)
wrf.write(‘test01’ + ‘\n’)
wrf.close()

txt = ‘landmark.txt’
wrf = open(txt, ‘a’)
wrf.write(‘test02’ + ‘\n’)
wrf.close()
结果:
test01
test02


版权声明:本文为yinwenbin0805原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。