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 解決selenium 中的 .clear()方法失效問(wèn)題
這篇文章主要介紹了python 解決selenium 中的 .clear()方法失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09對(duì)python numpy數(shù)組中冒號(hào)的使用方法詳解
下面小編就為大家分享一篇對(duì)python numpy數(shù)組中冒號(hào)的使用方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04利用Python探測(cè)附近WIFI密碼的詳細(xì)代碼
這篇文章主要介紹了利用Python探測(cè)附近WIFI密碼,基于python腳本實(shí)現(xiàn)wifi密碼的暴力破解從而實(shí)現(xiàn)免費(fèi)蹭網(wǎng),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01解決Python中定時(shí)任務(wù)線程無(wú)法自動(dòng)退出的問(wèn)題
今天小編就為大家分享一篇解決Python中定時(shí)任務(wù)線程無(wú)法自動(dòng)退出的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02極速整理文件Python自動(dòng)化辦公實(shí)用技巧
當(dāng)涉及到自動(dòng)化辦公和文件整理,Python確實(shí)是一個(gè)強(qiáng)大的工具,在這篇博客文章中,將深入探討極速整理文件!Python自動(dòng)化辦公新利器這個(gè)話題,并提供更加豐富和全面的示例代碼,以便讀者更好地理解和運(yùn)用這些技巧2024-01-01Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài)
這篇文章主要介紹了Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài),下面文章圍繞Python面向?qū)ο笕筇卣鞯南嚓P(guān)資料展開具體內(nèi)容,需要的朋友可以參考一下,希望對(duì)大家有所幫助2021-11-11初學(xué)者快看,Python下劃線的五個(gè)作用介紹
大家好,本篇文章主要講的是初學(xué)者快看,Python下劃線的五個(gè)作用介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12