Python文件操作中进行字符串替换的方法(保存到新文件/当前文件)


Posted in Python onJune 28, 2019

题目:

1.首先将文件:/etc/selinux/config 进行备份 文件名为 /etc/selinux/config.bak

2.再文件:/etc/selinux/config 中的enforcing 替换为 disabled

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#   enforcing - SELinux security policy is enforced.
#   permissive - SELinux prints warnings instead of enforcing.
#   disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#   targeted - Targeted processes are protected,
#   minimum - Modification of targeted policy. Only selected processes are protected. 
#   mls - Multi Level Security protection.
SELINUXTYPE=enforcing

•方法一:用replace

import os
import shutil
def selinux_config():
  """
  关闭SELINUX
  修改文件内容
  :return:
  """
  file_selinux = '/etc/selinux/config'
  backup_file_selinux = file_selinux + '.bak'
  temp_file_selinux = file_selinux + '.temp'
  if not os.path.exists(backup_file_selinux):
    shutil.copy2(file_selinux, backup_file_selinux)
    with open(file_selinux, mode='r') as fr, open(temp_file_selinux, mode='w') as fw:
      origin_line = 'SELINUX=enforcing'
      update_line = 'SELINUX=disabled'
      for line in fr:
        fw.write(line.replace(origin_line, update_line))
    os.remove(file_selinux)
    os.rename(temp_file_selinux, file_selinux)
if __name__ == '__main__':
  selinux_config()

•方法二:用re.sub

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import shutil
def selinux_config():
  """
  关闭SELINUX
  修改文件内容
  :return:
  """
  file_selinux = '/etc/selinux/config'
  backup_file_selinux = file_selinux + '.bak'
  temp_file_selinux = file_selinux + '.temp'
  if not os.path.exists(backup_file_selinux):
    shutil.copy2(file_selinux, backup_file_selinux)
    with open(file_selinux, mode='r') as fr, open(temp_file_selinux, mode='w') as fw:
      origin_line = 'SELINUX=enforcing'
      update_line = 'SELINUX=disabled'
      for line in fr:
        re_sub_list = re.sub(origin_line, update_line, line) # 这里用re.sub进行替换后放入 re_sub_list中
        fw.writelines(re_sub_list) # 将列表中的每一行进行写入。writelines是将序列对象中的每一行进行写入。
    os.remove(file_selinux)
    os.rename(temp_file_selinux, file_selinux)
if __name__ == '__main__':
  selinux_config()

总结

以上所述是小编给大家介绍的Python文件操作中进行字符串替换的方法(保存到新文件/当前文件) ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

Python 相关文章推荐
python设置windows桌面壁纸的实现代码
Jan 28 Python
在Python中用keys()方法返回字典键的教程
May 21 Python
详解Python网络爬虫功能的基本写法
Jan 28 Python
Python编程中对super函数的正确理解和用法解析
Jul 02 Python
Python设计模式之观察者模式原理与用法详解
Jan 16 Python
python3+pyqt5+itchat微信定时发送消息的方法
Feb 20 Python
Python爬虫beautifulsoup4常用的解析方法总结
Feb 25 Python
关于PyTorch源码解读之torchvision.models
Aug 17 Python
python matplotlib 画dataframe的时间序列图实例
Nov 20 Python
python如何把字符串类型list转换成list
Feb 18 Python
Python虚拟环境库virtualenvwrapper安装及使用
Jun 17 Python
python实现简单的五子棋游戏
Sep 01 Python
python打开windows应用程序的实例
Jun 28 #Python
python中PS 图像调整算法原理之亮度调整
Jun 28 #Python
Python中的 is 和 == 以及字符串驻留机制详解
Jun 28 #Python
Python实现 PS 图像调整中的亮度调整
Jun 28 #Python
Python绘图Matplotlib之坐标轴及刻度总结
Jun 28 #Python
python启动应用程序和终止应用程序的方法
Jun 28 #Python
简单了解python高阶函数map/reduce
Jun 28 #Python
You might like
第三节--定义一个类
2006/11/16 PHP
PHP 开发环境配置(Zend Server安装)
2010/04/28 PHP
修改PHP的memory_limit限制的方法分享
2012/02/21 PHP
ueditor 1.2.6 使用方法说明
2013/07/24 PHP
php获取数组长度的方法(有实例)
2013/10/27 PHP
浅析PHP程序设计中的MVC编程思想
2014/07/28 PHP
PHP连接MySQL数据的操作要点
2015/03/20 PHP
使用GD库生成带阴影文字的图片
2015/03/27 PHP
试用php中oci8扩展
2015/06/18 PHP
Laravel执行migrate命令提示:No such file or directory的解决方法
2016/03/16 PHP
js substr、substring和slice使用说明小记
2011/09/15 Javascript
Ext JS 4官方文档之三 -- 类体系概述与实践
2012/12/16 Javascript
js正则表达式中test,exec,match方法的区别说明
2014/01/29 Javascript
node.js入门教程之querystring模块的使用方法
2017/02/27 Javascript
基于jQuery的表单填充实例
2017/08/22 jQuery
强大的JavaScript响应式图表Chartist.js的使用
2017/09/13 Javascript
详解Vue webapp项目通过HBulider打包原生APP
2018/06/29 Javascript
[01:31]DOTA2上海特级锦标赛 SECRET战队完整宣传片
2016/03/16 DOTA
基于进程内通讯的python聊天室实现方法
2015/06/28 Python
初学python的操作难点总结(新手必看篇)
2017/08/03 Python
Python 网页解析HTMLParse的实例详解
2017/08/10 Python
利用selenium 3.7和python3添加cookie模拟登陆的实现
2017/11/20 Python
Python异常的检测和处理方法
2018/10/26 Python
Python解压 rar、zip、tar文件的方法
2019/11/19 Python
Python打印特殊符号及对应编码解析
2020/05/07 Python
Python如何脚本过滤文件中的注释
2020/05/27 Python
收集的7个CSS3代码生成工具
2010/04/17 HTML / CSS
Wiggle中国:英国骑行、跑步、游泳 & 铁三运动装备专卖网店
2016/08/02 全球购物
怎么写好自荐信
2013/10/30 职场文书
人事经理岗位职责
2014/04/28 职场文书
小学生读书活动总结
2014/06/30 职场文书
员工试用期转正自我评价
2015/03/10 职场文书
植树节新闻稿
2015/07/17 职场文书
升学宴学生致辞
2015/09/29 职场文书
Python可视化学习之matplotlib内置单颜色
2022/02/24 Python
Apache Hudi 加速传统的批处理模式
2022/04/24 Servers