首页 > 代码库 > python fileinput 模块替换文件全局内容

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,



本文出自 “梦想照进现实” 博客,请务必保留此出处http://lookingdream.blog.51cto.com/5177800/1923319

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