Python实现替换文件中指定内容的方法
作者:快递小可 时间:2023-02-23 06:26:03
本文实例讲述了Python实现替换文件中指定内容的方法。分享给大家供大家参考,具体如下:
这里使用python编写的程序,实现如下功能:将文件中的指定子串 修改为 另外的子串
编写的python程序,文件名是file_replace.py,具体代码如下:
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import sys,os
if len(sys.argv)<4 or len(sys.argv)>5:
sys.exit('There needs four or five parameters')
elif len(sys.argv)==4:
print 'usage:./file_replace.py old_text new_text filename'
else:
print 'usage:./file_replace.py old_text new_text filename --bak'
old_text,new_text=sys.argv[1],sys.argv[2]
file_name=sys.argv[3]
f=file(file_name,'rb')
new_file=file('.%s.bak' % file_name,'wb')#文件名以.开头的文件是隐藏文件
for line in f.xreadlines():#f.xreadlines()返回一个文件迭代器,每次只从文件(硬盘)中读一行
new_file.write(line.replace(old_text,new_text))
f.close()
new_file.close()
if '--bak' in sys.argv: #'--bak'表示要求对原文件备份
os.rename(file_name,'%s.bak' % file_name) #unchanged
os.rename('.%s.bak' % file_name,file_name) #changed
else:
os.rename(file_name,'wahaha.txt')#此处也可以将原文件删除,以便下一语句能够正常执行
os.rename('.%s.bak' % file_name,file_name)
下面是代码执行的一个例子:
song@ubuntu:~$ more hello.txt
Hello python
Hello world
python Hello
world Hello
song@ubuntu:~$ python file_replace.py Hello love hello.txt --bak
usage:./file_replace.py old_text new_text filename --bak
song@ubuntu:~$ ls
Desktop Documents file_replace.py Music systemExit.py
diff1.txt Downloads hello.txt Pictures Templates
diff.txt examples.desktop hello.txt.bak Public Videos
song@ubuntu:~$ more hello.txt
love python
love world
python love
world love
song@ubuntu:~$ more hello.txt.bak
Hello python
Hello world
python Hello
world Hello
song@ubuntu:~$
更多Python相关内容感兴趣的读者可查看本站专题:《Python字符串操作技巧汇总》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
来源:http://blog.csdn.net/sxingming/article/details/51924401
标签:Python,替换,文件内容
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySql常用操作SQL语句汇总
2024-01-21 00:11:53
Python安装第三方库的3种方法
2022-02-03 03:10:47
为您解读CSS优先级
2009-06-18 18:29:00
![](https://img.aspxhome.com/file/UploadPic/20096/18/01-66s.gif)
python 实现单一数字取对数与数列取对数
2023-04-02 01:38:14
![](https://img.aspxhome.com/file/2023/3/96963_0s.png)
SQL Server中元数据函数的用法
2024-01-20 16:26:54
python实现守护进程、守护线程、守护非守护并行
2021-02-03 09:06:56
Javascript优化技巧(文件瘦身篇)
2024-04-22 13:04:16
python处理Excel xlrd的简单使用
2023-11-29 04:13:32
详解Django中views数据查询使用locals()函数进行优化
2021-01-21 00:53:13
MySql索引原理与操作
2024-01-28 22:27:31
![](https://img.aspxhome.com/file/2023/6/126246_0s.png)
深入浅析JavaScript中的arguments对象(强力推荐)
2024-04-25 13:12:46
SQL Server 排序函数 ROW_NUMBER和RANK 用法总结
2024-01-20 17:16:36
纯CSS去除按钮以及链接点击时虚线
2009-05-21 10:40:00
vue 封装面包屑组件教程
2024-05-02 17:11:11
![](https://img.aspxhome.com/file/2023/2/130032_0s.jpg)
Pytorch 如何加速Dataloader提升数据读取速度
2023-11-12 00:00:32
![](https://img.aspxhome.com/file/2023/6/115196_0s.jpg)
VS2019连接mysql8.0数据库的教程图文详解
2024-01-13 12:54:26
![](https://img.aspxhome.com/file/2023/6/107906_0s.jpg)
CSS实现元素透明的那些事
2009-09-07 12:22:00
pandas.DataFrame的for循环迭代的实现
2023-07-07 22:10:28
js模仿php中strtotime()与date()函数实现方法
2024-04-18 10:01:55
Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程
2024-05-11 10:46:59