一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件)

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

2021-07-26 00:20A72HONGJIE Python

這篇文章主要介紹了Python文件操作中進(jìn)行字符串替換的方法(保存到新文件/當(dāng)前文件) ,本文給大家介紹兩種方法,每種方法給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

題目:

1.首先將文件:/etc/selinux/config 進(jìn)行備份 文件名為 /etc/selinux/config.bak

2.再文件:/etc/selinux/config 中的enforcing 替換為 disabled

?
1
2
3
4
5
6
7
8
9
10
11
# 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#! /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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

原文鏈接:https://www.cnblogs.com/a72hongjie/articles/9161913.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区国产二区国产三区 | 夫妻性生活在线 | 男人的天堂视频 | 操人网| 成人精品一区久久久久 | 四虎影业 | 日韩精品亚洲专区在线影视 | 草莓香蕉绿巨人丝瓜榴莲污在线观看 | 福利一区三区 | 侮辱丰满美丽的人妻 | meyd—447佐山爱在线 | 91麻豆精品国产自产在线观看 | 精品视频在线观看免费 | 动漫美女羞羞视频 | 国产精品国产国产aⅴ | 天天操天天射天天色 | 深夜日韩 | 99在线精品日韩一区免费国产 | 满城尽带黄金甲大胸片 | 大陆国语自产精品视频在 | 国模孕妇季玥全部人体写真 | 人妖欧美一区二区三区四区 | 青青五月天 | 成年人视频在线播放 | 91国内精品久久久久影院优播 | 亚洲黄色色图 | 好爽好粗 | 午夜私人福利影院 | 法国老妇性xx在线播放 | 亚洲电影第1页 | 久久成人国产精品一区二区 | 欧美一区二区三区成人看不卡 | 久久r视频| 短篇艳妇系列 | 国产成+人+综合+欧美 亚洲 | 为什么丈夫插我我却喜欢被打着插 | 免费一级毛片在线播放 | 好大好深受不了了快进来 | 波多野结衣护士 | 日本红色高清免费观看 | 日本免费一区二区三区四区五六区 |