python修改文件内容的3种方法详解
作者:wc_chan 时间:2021-07-27 10:19:36
这篇文章主要介绍了python修改文件内容的3种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
一、修改原文件方式
def alter(file,old_str,new_str):
"""
替换文件中的字符串
:param file:文件名
:param old_str:就字符串
:param new_str:新字符串
:return:
"""
file_data = ""
with open(file, "r", encoding="utf-8") as f:
for line in f:
if old_str in line:
line = line.replace(old_str,new_str)
file_data += line
with open(file,"w",encoding="utf-8") as f:
f.write(file_data)
alter("file1", "09876", "python")
二、把原文件内容和要修改的内容写到新文件中进行存储的方式
2.1 python字符串替换的方法,修改文件内容
import os
def alter(file,old_str,new_str):
"""
将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原来文件的名字
:param file: 文件路径
:param old_str: 需要替换的字符串
:param new_str: 替换的字符串
:return: None
"""
with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
for line in f1:
if old_str in line:
line = line.replace(old_str, new_str)
f2.write(line)
os.remove(file)
os.rename("%s.bak" % file, file)
alter("file1", "python", "测试")
2.2 python 使用正则表达式 替换文件内容 re.sub 方法替换
import re,os
def alter(file,old_str,new_str):
with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
for line in f1:
f2.write(re.sub(old_str,new_str,line))
os.remove(file)
os.rename("%s.bak" % file, file)
alter("file1", "admin", "password")
来源:https://www.cnblogs.com/wc-chan/p/8085452.html
标签:python,修改,文件,内容,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
GoLang调用链可视化go-callvis使用介绍
2023-07-16 06:39:59
![](https://img.aspxhome.com/file/2023/5/89025_0s.png)
使用keras实现densenet和Xception的模型融合
2022-04-15 19:17:01
![](https://img.aspxhome.com/file/2023/1/77901_0s.jpg)
JS如何获取变量值
2008-05-18 12:52:00
pytorch实现focal loss的两种方式小结
2023-07-02 14:43:22
Python爬虫之Selenium警告框(弹窗)处理
2021-10-06 23:59:12
有趣的python小程序分享
2023-11-27 20:31:55
![](https://img.aspxhome.com/file/2023/7/98787_0s.png)
ASP图片分页代码 (通用)
2009-06-22 12:57:00
python的字典和集合你了解吗
2022-12-13 11:28:44
![](https://img.aspxhome.com/file/2023/0/65550_0s.png)
asp fso删除非空目录的例子
2008-11-26 20:03:00
通过Turtle库在Python中绘制一个鼠年福鼠
2021-03-01 03:48:12
![](https://img.aspxhome.com/file/2023/2/69462_0s.gif)
一种特别简单的MySQL数据库安装方法
2008-12-17 15:30:00
利用Python+OpenCV三步去除水印
2021-09-17 10:32:30
![](https://img.aspxhome.com/file/2023/7/80617_0s.jpg)
Python 经典贪心算法之Prim算法案例详解
2024-01-01 18:18:27
![](https://img.aspxhome.com/file/2023/2/79482_0s.png)
Python爬虫基础初探selenium
2023-07-26 17:40:50
![](https://img.aspxhome.com/file/2023/4/61194_0s.png)
Python序列对象与String类型内置方法详解
2023-09-22 13:25:18
网页栅格系统研究(4):技术实现
2008-11-06 11:44:00
![](https://img.aspxhome.com/file/UploadPic/200811/6/blueprint_3cols-59s.png)
Go语言题解LeetCode1266访问所有点的最小时间示例
2023-08-29 08:10:39
![](https://img.aspxhome.com/file/2023/1/99051_0s.jpg)
Python实现将16进制字符串转化为ascii字符的方法分析
2023-11-17 12:26:25
PHP MVC模式在网站架构中的实现分析
2023-07-22 21:05:14
![](https://img.aspxhome.com/file/2023/6/55516_0s.gif)
Javascript DOM 编程实例讲解--仿LightBox效果提示框
2008-05-01 13:25:00
![](https://img.aspxhome.com/file/UploadPic/up/2008050116095611.gif)