python fileinput 模块替换文件全局内容

简介:

nginx.conf 文件有如下内容:

 server  {    .....................................................................   

                       server 192.168.201.131:80 weight=1;

server 192.168.201.132:80 weight=1;

server 192.168.201.133:80 weight=1;

                        server 192.168.201.136:80 weight=1;

server 192.168.201.137:80 weight=1;

server 192.168.201.139:80 weight=1;

server 192.168.205.2:80 weight=1;

          server 192.168.205.3:80 weight=1;

         server 192.168.205.4:80 weight=1;

         server 192.168.205.5:80 weight=1;

         server 192.168.205.6:80 weight=1;

         server 192.168.205.7:80 weight=1;

         server 192.168.205.8:80 weight=1;  

         server 192.168.205.9:80 weight=1;  

                        server 192.168.205.10:80 weight=1;  

                        server 192.168.205.11:80 weight=1;  

                        server 192.168.205.12:80 weight=1;  

                        server 192.168.205.13:80 weight=1;  

                        server 192.168.205.14:80 weight=1;  

                        server 192.168.205.15:80 weight=1;  

                        server 192.168.205.16:80 weight=1;  

                      server 192.168.205.17:80 weight=1;  

                      server 192.168.205.18:80 weight=1;  

                      server 192.168.205.19:80 weight=1;  

                      server 192.168.205.20:80 weight=1;  

                      server 192.168.205.21:80 weight=1;

                      server 192.168.205.22:80 weight=1;

                      server 192.168.205.23:80 weight=1;

                      server 192.168.205.24:80 weight=1;

                      server 192.168.205.25:80 weight=1;

                      server 192.168.205.26:80 weight=1;

                      server 192.168.205.27:80 weight=1;

                      server 192.168.205.28:80 weight=1;

                      server 192.168.205.29:80 weight=1;

                      server 192.168.205.30:80 weight=1;

                      server 192.168.205.31:80 weight=1;

                      server 192.168.205.32:80 weight=1;

                      server 192.168.205.33:80 weight=1;

                      server 192.168.205.34:80 weight=1;

                      server 192.168.205.35:80 weight=1;

                      server 192.168.205.36:80 weight=1;

                      server 192.168.205.37:80 weight=1;

                      server 192.168.205.38:80 weight=1;

                      server 192.168.205.39:80 weight=1;

                      server 192.168.205.40:80 weight=1;

                      server 192.168.205.41:80 weight=1;

}

在上面所有205网段上面加上#注销号,python 实现脚本如下:

#!/bin/python

#coding=utf-8

import fileinput

for line in fileinput.input('nginx.conf.erb201',inplace=1):

     line = line.replace('server 192.168.205.','#server 192.168.205.')

     print line,





     本文转自yzy121403725 51CTO博客,原文链接:http://blog.51cto.com/lookingdream/1923319,如需转载请自行联系原作者



相关文章
|
14天前
|
安全 项目管理 Python
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
|
2天前
|
Java 数据安全/隐私保护 Python
Python案例分享:如何实现文件的解压缩
Python案例分享:如何实现文件的解压缩
21 8
|
2天前
|
存储 缓存 安全
Python案例分享:如何实现文件的上传下载
Python案例分享:如何实现文件的上传下载
23 6
|
2天前
|
Java Serverless Python
探索Python中的并发编程与`concurrent.futures`模块
探索Python中的并发编程与`concurrent.futures`模块
10 4
|
14天前
|
Python
如何在 Python 中导入模块
【8月更文挑战第29天】
18 1
|
14天前
|
Python
|
14天前
|
数据采集 JSON 算法框架/工具
我常用的几个经典Python模块
我常用的几个经典Python模块
|
14天前
|
开发者 Python
什么是 python 模块?
【8月更文挑战第29天】
9 0
|
14天前
|
TensorFlow 算法框架/工具 C++
Python exe文件打包神器-Nuitka!
Python exe文件打包神器-Nuitka!
|
14天前
|
存储 安全 Python
Python中临时文件的妙用
Python中临时文件的妙用