python写文件时覆盖原来的实例方法
作者:尤及 时间:2021-06-01 05:21:51
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()
结果:
test01
test02
来源:https://www.py.cn/jishu/jichu/19248.html
标签:python,覆盖文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php session 预定义数组
2023-11-15 01:15:22
Python中with...as...的使用方法
2021-10-01 16:58:07
![](https://img.aspxhome.com/file/2023/4/81554_0s.png)
python实现单目标、多目标、多尺度、自定义特征的KCF跟踪算法(实例代码)
2022-03-31 11:23:43
Numpy中创建数组的9种方式小结
2021-03-25 21:39:43
![](https://img.aspxhome.com/file/2023/7/103247_0s.png)
python下如何让web元素的生成更简单的分析
2023-01-28 23:46:34
学习ASP.NET八天入门:第六天
2007-08-07 13:48:00
![](https://img.aspxhome.com/file/UploadPic/20078/7/200787135226497s.gif)
Ewebeditor及fckeditork单引号的问题
2010-04-05 21:12:00
做新产品?悠着点
2009-03-01 13:10:00
一文了解Python并发编程的工程实现方法
2023-12-26 13:54:36
python 对给定可迭代集合统计出现频率,并排序的方法
2023-02-10 17:28:47
PyTorch中permute的基本用法示例
2022-04-22 01:14:01
字符,字节和编码
2009-12-09 15:59:00
![](https://img.aspxhome.com/file/UploadPic/200912/9/spacer-36s.gif)
聊天室php&mysql(五)
2023-11-19 20:18:22
python实现统计代码行数的方法
2021-06-23 11:24:17
09七夕节各大搜索引擎LOGO欣赏
2009-08-27 15:34:00
![](https://img.aspxhome.com/file/UploadPic/20098/27/09-tanabata-festival-search-logo-44s.jpg)
python实现二维数组的对角线遍历
2023-05-15 06:08:01
![](https://img.aspxhome.com/file/2023/8/115218_0s.gif)
标签明晰、有效
2010-01-18 12:22:00
个人从事设计行业40句观感
2008-04-07 13:58:00
![](https://img.aspxhome.com/file/UploadPic/20084/7/2008471415284s.jpg)
如何确定Oracle数据库表重复的记录
2010-07-26 13:23:00
Python写的一个简单DNS服务器实例
2021-03-25 21:13:47