Python

作者: 乂滥好人 | 来源:发表于2018-11-08 10:16 被阅读11次

    1、change Keyword

    #encoding:utf-8 # 支持中文输入
    import re
    import os
    s = os.sep
    
    ##获取当前工作目录
    root = os.path.abspath('.')
    #需要忽略的文件后缀
    ignoredTrail = [".py"]
    #需要忽略的文件夹
    ignoredDirPath = ["/Users/fang/Downloads/Demo/Test"]
    #原来的前缀
    oldPrefix="new_"
    #新前缀
    newPrefix="change_"
    
    #获取文件名和后缀
    def getFileNameAndExt(filename):
     (filepath,tempfilename) = os.path.split(filename);
     (shotname,extension) = os.path.splitext(tempfilename);
     return shotname,extension
    
    #是否是需要根据后缀忽略的文件
    def isIgnoredByTrail(filePath):
      file = getFileNameAndExt(filePath)
      for trail in ignoredTrail:
        if file[1] == trail:
          print "忽略文件:" + filePath
          return bool(1)
      return bool(0)
      
    #是否根据文件夹路径忽略
    def isIgnoredByDirPath(dirpath):
      for path in ignoredDirPath:
        if dirpath == path:
          print "忽略目录:" + path
          return bool(1)
      return bool(0)
    
    #获取所有文件名
    def getAllFiles(path):
     allfile=[]
     for dirpath,dirnames,filenames in os.walk(path):
      
      if isIgnoredByDirPath(dirpath):
        continue
      for dir in dirnames:
       allfile.append(os.path.join(dirpath,dir))
      for name in filenames:
       allfile.append(os.path.join(dirpath, name))
     return allfile
    
    #重命名
    def rename(oldName,newName,filePath):
      a = open(filePath,'r') #打开所有文件
      str = a.read()
    
      str = str.replace(oldName,newName)#将字符串里前面全部替换为后面
      b = open(filePath,'w')
      b.write(str) #再写入
      b.close() #关闭文件
      print "+++++++++++" + filePath + "finished"
    
    
    fileList = getAllFiles(root)
    for file in fileList:
      if os.path.isfile(file):
        if isIgnoredByTrail(file):
          continue
        rename(oldPrefix,newPrefix,file)
    
    

    2、change class prefix

    #!/usr/bin/env python\
    # -*- coding: utf-8 -*-
    
    import os
    
    
    for dirpath, _, filenames in os.walk('.'):
        for filename in filenames:
            if filename.startswith('KXH'):
                oldFile = os.path.join(dirpath, filename)
                newFile = os.path.join(dirpath, filename.replace('KXH', 'Change_', 2))
                print newFile
                inFile = open(oldFile)
                outFile = open(newFile, 'w')
                replacements = {'KXH':'Change_'}
                for line in inFile:
                    for src, target in replacements.iteritems():
                        line = line.replace(src, target)
                    outFile.write(line)
                inFile.close()
                outFile.close()
                os.remove(oldFile)
    

    3、tengteng change

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import os
    
    
    #文件名列表
    filelist = ['Change_Constant.h', 'Change_Constant.m']
    #原关键字
    src_keywords = ['_k', 'NEW']
    #新关键字
    dest_keywords = ['_s', 'FC']
    
    
    def main():
        for dirpath, dirnames, filenames in os.walk('.'):
        for filename in filenames:
            if filename in filelist:
            #获得文件相对路径
                    filepath = os.path.join(dirpath, filename)
                    #print 'filepath: ' + filepath
                    change_keywords(filepath, src_keywords ,dest_keywords)
    
    
    def change_keywords(filepath, src_keywords, dest_keywords):
        content_old = open(filepath, 'r')
        content_new = ''
        code_list = content_old.readlines()
        for code in code_list:
        i = 0
            for i in range(len(src_keywords)): 
                code = code.replace(src_keywords[i], dest_keywords[i])
                i += 1  
                print  'change to: ' + code
            #生成新的文件内容
            content_new += code
        content_old.close()
        #写入源文件并保存
        file_new = open(filepath, 'w')
        file_new.writelines(content_new)
        file_new.close()
    
    
    main()
    

    相关文章

      网友评论

          本文标题:Python

          本文链接:https://www.haomeiwen.com/subject/pamixqtx.html