Python文件操作中进行字符串替换的方法(保存到新文件/当前文件)
2019-06-28 11:46
417 查看
题目:
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文件操作小练习之写替换文件里的个别字符(保存到新文件/当前文件)
- python3.6—将文件(record.txt)中的数据进行分割并分别保存(附录:文件打开模式,文件对象方法以及字符串使用及注释)
- python基础_文件操作实现全文或单行替换的方法
- python反转字符串(简单方法)及简单的文件操作示例
- cmd运行python文件时对结果进行保存的方法
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用bs4,beautifulsoup模块
- python保存字符串到文件的方法
- Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
- python读配置文件;正则表达式替换、切割字符串;获取当前时间
- python---布尔类型赋值,字符串,len(),转义、拼接、替换、文件操作
- Python文件进行写操作后立即读出的结果、原因分析、解决方法
- 利用Python+opencv进行视频文件的读取和保存,打开笔记本摄像头拍照保存、图像在窗口显示等操作
- python对配置文件.ini进行增删改查操作的方法示例
- Python实现对PPT文件进行截图操作的方法
- python文件操作--字符串替换
- Python文件操作:同一个文件进行内容替换
- python对文件进行修改替换操作
- Python---对html文件内容进行搜索取出特定URL地址字符串,保存成列表,并使用每个url下载图片,并保存到硬盘上,使用正则re
- python保存字符串到文件的方法
- 批处理文件操作:替换文本文件某系字符写入新文件、遍历目录并进行字符串替换、拷贝文件到新目录、去掉文件扩展名