日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件)

系統(tǒng) 1779 0

題目:

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是將序列對象中的每一行進(jìn)行寫入。
    os.remove(file_selinux)
    os.rename(temp_file_selinux, file_selinux)
if __name__ == '__main__':
  selinux_config()
          

總結(jié)

以上所述是小編給大家介紹的Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件) ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 佳木斯市| 万盛区| 都安| 介休市| 黔西县| 报价| 绥滨县| 大同市| 栖霞市| 福安市| 泰州市| 靖宇县| 青海省| 辉南县| 康定县| 阿拉善右旗| 红桥区| 阿坝县| 河东区| 曲阜市| 碌曲县| 温州市| 南召县| 三都| 林甸县| 霍邱县| 岳阳县| 大埔区| 上虞市| 连云港市| 萝北县| 车险| 石景山区| 化隆| 伊宁市| 和静县| 镇远县| 洞口县| 汤阴县| 永新县| 忻州市|