Fork me on GitHub

import os
dirname="C:\\Users\\haier\\Desktop\\new"

def walk(path):
    for item in os.listdir(path):
        subpath = os.path.join(path, item)
        #subpath=subpath.replace('\\','\\\\')
        print subpath
        for i in open(subpath):
            pass#print i
        ffrom=open(subpath,'r')
        fto=open(subpath+"1",'w')
        while True:
            l=ffrom.readline()
            if not l:
                break
            if 'endif' in l:
                print l
                l=l.replace('endif','end')
                print l
            elif 'endwhile' in l:
                print l
                l=l.replace('endwhile','end')
                print l                
            # if '\"' in l:
                # print l
                # l=l.replace('\"','\'')
                # print l
              #  print l
               # tmp=l.split("\"")
                #l=tmp[0]+"\'"+tmp[1]
            fto.write(l)                      
        ffrom.close()
        fto.close()

def dele(path):
   for item in os.listdir(path):
        subpath = os.path.join(path, item)
       # subpath=subpath.replace('\\','\\\\')

            
        if os.path.exists(subpath+"1"):
            ffrom=open(subpath+"1",'r')
            fto=open(subpath,'w')
            while True:
                l=ffrom.readline()
                if not l:
                    break
                fto.write(l)                      
            ffrom.close()
            fto.close()
            os.remove(subpath+"1")

        
walk(dirname)
dele(dirname)

 

posted on 2013-08-08 12:04  huashiyiqike  阅读(420)  评论(0编辑  收藏  举报