python在txt指定行添加文本(Python修改文件往指定行插入内容的实例)
类别:脚本大全 浏览量:515
时间:2022-04-02 10:22:14 python在txt指定行添加文本
Python修改文件往指定行插入内容的实例需求:批量修改py文件中的类属性,为类增加一个core = True新的属性
原py文件如下
a.py
|
class A(): description = "abc" |
现在有一个1.txt文本,内容如下,如果有py文件中的description跟txt文本中的一样,则增加core属性
1.txt
|
description = "abc" description = "123" |
实现思路:
1.需要遍历code目录下的所有py文件,然后读取所有行数内容保存到lines列表中
2.遍历每个文件的每一行,匹配1.txt中的description,如果匹配中,就返回行号
3.往lines列表中根据行号插入要增加的新属性
4.重新写回原文件,达到修改文件的目的
如果修改成功后,效果应该是这样的
a.py
|
class A(): description = "abc" core = True |
实现代码:
|
import os original_folder = 'E:\\code\\' core_list = [] count = 0 # if the description is in the current line def isMatchDescription(line_buffer): global core_list # if not catch the core_list in global, reload it. if not core_list: with open ( "./core.txt" , "r" ) as f: core_list = f.readlines() # if match the core description for des in core_list: if line_buffer.strip() = = des.strip(): return True return False def modifySignatures(): for dirpath, dirnames, filenames in os.walk(original_folder): for filename in filenames: modifyFile(os.path.join(dirpath,filename)) def modifyFile(filename): global count #print "Current file: %s"% filename lines = [] with open (filename, "r" ) as f: lines = f.readlines() hit = 0 # Enume every single line for match the description for index, line in enumerate (lines): if isMatchDescription(line): hit = index print hit print "Matched file:%s" % filename count + = 1 if hit > 0 : lines.insert(hit - 1 , ' core = True\n' ) f.close() # Write back to file with open (filename, "w" ) as f: for line in lines: f.write(line) f.close() if __name__ = = '__main__' : modifySignatures() print "Modified:%d" % count |
代码中的lines.insert(hit-1,' core = True\n')这一行,hit代表目标py文件的description属性的行号,我之前用的是hit+1,但是后面发现有些文件出现了语法错误,原因是py文件中有些description的值太长,导致原文件使用了代码换行符\,如下:
a.py
|
class A(): description = "abc\ aaaaabbbbb" |
这样的如果修改后就变成了
|
class A(): description = "abc\ core = True aaaaabbbbb" |
为了避免这个bug,后面我才改成了hit-1
|
lines.insert(hit - 1 , ' core = True\n' ) |
这样修改的py文件后就是这样的效果
|
class A(): core = True description = "abc\ aaaaabbbbb" |
以上这篇Python修改文件往指定行插入内容的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/cssxn/article/details/81003146
您可能感兴趣
- python如何使用yield(YII2.0框架行为Behavior深入详解)
- python中比较同一字典value(在Python 字典中一键对应多个值的实例)
- pythonmatplotlib怎么设置图像(完美解决Python matplotlib绘图时汉字显示不正常的问题)
- python读取mat文件(详解Python Matplot中文显示完美解决方案)
- python中tkinter模块窗口操作(详解python tkinter教程-事件绑定)
- python3d旋转特效(python实现小球弹跳效果)
- 详解Python locals()的陷阱(详解Python locals的陷阱)
- python对于gbk处理(Python 2/3下处理cjk编码的zip文件的方法)
- python把文件上传服务器(Python 实现两个服务器之间文件的上传方法)
- 怎么python爬取网页图片(详解Python静态网页爬取获取高清壁纸)
- python实现购物网站(Python实战购物车项目的实现参考)
- python处理tcp包(Python3使用TCP编写一个简易的文件下载器功能)
- python生成密码字典教程(Python利用字典破解WIFI密码的方法)
- python列表切片和分片(Python列表切片操作实例总结)
- python 正则表达式在代码里的使用(python中正则表达式与模式匹配)
- vscode如何配置python环境(VSCode Python开发环境配置的详细步骤)
- 高考数学题(高考数学题基础题占多少分)
- 没钱只能吃土(没钱要吃土了幽默短信发朋友圈)
- 今年考高会很难吗(今年高考会考试吗)
- 盘古开天地 他创造了世界,谁创造了盘古 盘古是伏羲吗(盘古开天地他创造了世界)
- 关于队徽 你了解这些么 二(关于队徽你了解这些么)
- 冬天来了手脚冰凉 真不是因为上辈子你是折翼的天使(冬天来了手脚冰凉)
热门推荐
- sqlserver查询自定义时间数据(SQLServer查询某个时间段购买过商品的所有用户)
- python如何将运行结果存入txt中(详解python读取和输出到txt)
- ros必须在ubuntu安装吗(详解ubuntu20.04下CLion2020.1.3安装配置ROS过程说明)
- 微信小程序接口返回数据怎么弄(微信小程序页面返回传值的4种解决方案汇总)
- ASP.NET中获取匿名对象的属性值
- Array.ConvertAll数组间转换
- python多线程有两个参数怎么传(python从子线程中获得返回值的方法)
- linux切换python版本(linux安装python修改默认python版本方法)
- mysql8.0.12安装教程图解(mysql 8.0.22 zip压缩包版免安装下载、安装配置步骤详解)
- mysql mvcc 流程(Mysql MVCC机制原理详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9