python寫文件時覆蓋原來的實例方法
python寫文件時覆蓋原來寫的方法:
使用“open(’文件名’,’w’)”語句,以寫模式打開文件,然后使用write函數寫文件
最后用close函數關閉打開的文件,文件原來的內容就會被覆蓋了
示例如下:
對文件操作之前的文件內容
對文件操作之后的文件內容
完整代碼如下:
file = open(’ss.txt’, ’w’)file.write(’123456789’)file.close()
知識點擴展:
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()
結果:
test01test02
到此這篇關于python寫文件時覆蓋原來的實例方法的文章就介紹到這了,更多相關python寫文件時如何覆蓋原來寫的內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
