批量处理文件

1个小工具: 

1、批量替换 Host www.mm.cn 到 macaroon.zymlinux.net

2、CaseName:作为当前文件的文件名保存文件。

3、下面的分布的steps批量替换成上方描述的steps,并且保持与#的0间隔缩进

4、uri替换成casename的尾段

import os
import sys
def doupdate():
    j=1
     for root, dirs, files in os.walk(os.curdir):
        if not files.startswith('case_rfc2616'):
            continue
        else:
            #print i
            update(i,j)
            j=j+1
            
def update(f,j):
    nf = f +'.new'
    fname = ""
    step = [ "abc" for i in range(10)]
    o_fd = open(f, 'r')
    n_fd = open(nf, 'w')
    for l in o_fd:
        if l.find('www.mm.cn') >= 0:
            n = l.replace('www.mm.cn','macaroon.zymlinux.net')
            n_fd.write(n)
        elif l.find('step ') >= 0:
            for i in range(9):
                if l.find("step %d:"%i)>=0:
                    print "find i ==============",i
                    if step[i]=="abc":
                        step[i] = l
                        n= l
                    else:
                        n = "#"+l.replace(l,step[i]).strip("#").strip()+"\n"
            n_fd.write(n)
        elif l.find('case_rfc2616_') >= 0:
            fname = l.strip("#").strip()
            print fname
            n_fd.write(l)
        elif l.find('UTF-8') >=0:
            n = l.replace('UTF-8', 'utf-8')
            n_fd.write(n)
        else:
            n_fd.write(l)
    n_fd.close()
    o_fd.close()
    if f<>nf:
        pass
        os.system('rm %s'%f)
    os.system("mv %s %s.yaml" % (nf, fname))
    
    f = open("result.log",'a')  
    f.write("%d. `%s.yaml`\n\t >\n"%(j,fname))
              
if __name__ == '__main__':
    doupdate()


      

你可能感兴趣的:(批量处理文件)