https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python

from tempfile import mkstemp
from shutil import move
from os import fdopen, remove def replace(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)

最新文章

  1. java终端获取输入
  2. NoSql数据库初探-mongoDB读操作
  3. LeetCode 21 -- Merge Two Sorted Lists
  4. Hibernate学习总结
  5. latch: cache buffers chains故障处理总结(转载)
  6. Eclipse格式化代码快捷键失效问题
  7. Hibernate出现javax.naming.NoInitialContextException 错误的解决办法
  8. 拦路虎:jQuery
  9. Cython:基础教程(1) 语法
  10. sun.misc.unsafe类的使用
  11. UVALive 7454 Parentheses (栈+模拟)
  12. 响应式Web图形篇 —— icon fonts 的探析及应用
  13. 加密Web.Config配置文件
  14. java中抽象类的概念
  15. 《JavaScript高级程序设计》笔记:变量、作用域和内存问题(四)
  16. 如何把开源项目发布到Jcenter
  17. Code optimization and organization in Javascript / jQuery
  18. [UE4]C++ getter and setter
  19. ILMerge-GUI的使用
  20. 10分钟教你用Python打造微信天气预报机器人

热门文章

  1. ajax请求以及递归
  2. python 控制台输出美化
  3. STM32F103ZET6通用定时器
  4. 关于selenium定位元素时,出现此问题的处理办法:find_element=wait.until(ec.presence_of_element_locatetd(locator))定位不到页面元素的问题
  5. 【公告】请访问我Blog新站——superman2014 www.superman2014.com
  6. 中阶 d05 tomcat 安装 eclipse上配置tomcat
  7. springboot web项目创建及自动配置分析(thymeleaf+flyway)
  8. matplotlib Transform
  9. matplotlib TransformNode类
  10. python如何操作excel 基础代码