脚本专栏 
首页 > 脚本专栏 > 浏览文章

python写文件时覆盖原来的实例方法

(编辑:jimmy 日期: 2024/5/20 浏览:3 次 )

python写文件时覆盖原来写的方法:

使用“open('文件名','w')”语句,以写模式打开文件,然后使用write函数写文件

最后用close函数关闭打开的文件,文件原来的内容就会被覆盖了

示例如下:

对文件操作之前的文件内容

python写文件时覆盖原来的实例方法

对文件操作之后的文件内容

python写文件时覆盖原来的实例方法

完整代码如下:

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()

结果:

test01
test02

上一篇:Python中Selenium库使用教程详解
下一篇:python中return不返回值的问题解析
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网