Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件)
題目:
1.首先將文件:/etc/selinux/config 進(jìn)行備份 文件名為 /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():
"""
關(guān)閉SELINUX
修改文件內(nèi)容
: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():
"""
關(guān)閉SELINUX
修改文件內(nèi)容
: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進(jìn)行替換后放入 re_sub_list中
fw.writelines(re_sub_list) # 將列表中的每一行進(jìn)行寫入。writelines是將序列對(duì)象中的每一行進(jìn)行寫入。
os.remove(file_selinux)
os.rename(temp_file_selinux, file_selinux)
if __name__ == '__main__':
selinux_config()
總結(jié)
以上所述是小編給大家介紹的Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件) ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Python升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法
這篇文章主要給大家介紹了因?yàn)镻ython升級(jí)導(dǎo)致yum、pip報(bào)錯(cuò)的解決方法,文中通過(guò)示例代碼將解決的方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
python使用socket進(jìn)行簡(jiǎn)單網(wǎng)絡(luò)連接的方法
這篇文章主要介紹了python使用socket進(jìn)行簡(jiǎn)單網(wǎng)絡(luò)連接的方法,實(shí)例分析了Python使用socket的基本技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Python獲取接口數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了Python獲取接口數(shù)據(jù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Windows11使用Cpython?編譯文件報(bào)錯(cuò)?error:?Unable?to?find?vcvars
這篇文章主要介紹了Windows11使用Cpython編譯文件報(bào)錯(cuò)error:Unable?to find?vcvarsall.bat完美解決方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
利用pandas進(jìn)行數(shù)據(jù)清洗的方法
本文主要介紹了利用pandas進(jìn)行數(shù)據(jù)清洗的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Jupyter Notebook內(nèi)使用argparse報(bào)錯(cuò)的解決方案
這篇文章主要介紹了在Jupyter Notebook內(nèi)使用argparse報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Python腳本實(shí)現(xiàn)隨機(jī)數(shù)據(jù)生成自由詳解
這篇文章主要為大家詳細(xì)介紹了Python如何通過(guò)腳本實(shí)現(xiàn)隨機(jī)數(shù)據(jù)生成自由,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2023-12-12
利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過(guò)python對(duì)數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09
python按列索引提取文件夾內(nèi)所有excel指定列匯總(示例代碼)
這篇文章主要介紹了python按列索引提取文件夾內(nèi)所有excel指定列匯總,本文通過(guò)多種場(chǎng)景分析結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
Python實(shí)現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)處理apiDoc轉(zhuǎn)swagger的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-02-02

