[Python] 批量去除UTF8的Bom标签 →→→→→进入此内容的聊天室

来自 , 2021-03-08, 写在 Python, 查看 141 次.
URL http://www.code666.cn/view/f7b6bc88
  1. #!/usr/bin/python
  2. #coding=utf-8
  3.  
  4. import os
  5. import sys
  6. import codecs
  7.  
  8. class RemoveBom:  
  9.  
  10.     basePath = ''
  11.     fileList = []
  12.     trimExtList = []
  13.    
  14.     def showMessages(self):
  15.         print 'the Path is [',self.basePath,']'
  16.         n = ''
  17.         for ext in self.trimExtList:
  18.             n+=ext
  19.             n+=' '
  20.         print 'the Exts is [' ,n,']'
  21.  
  22.     def trimFile(self,name):
  23.         file = open(name,'rb')
  24.         content = file.read(3)
  25.         if content != '\xEF\xBB\xBF':
  26.             return False
  27.         content = file.read()
  28.         file.close()
  29.         file = open(name,'wb')
  30.         file.write(content)
  31.         file.close
  32.         print 'convert ',name,' finish'
  33.         return True
  34.  
  35.     def getFileList(self,path):
  36.         if not path:
  37.             return False
  38.         for root,dirs,files in os.walk(path):
  39.             for filename in files:
  40.                 if filename.split('.')[-1] in self.trimExtList:
  41.                         filepath=os.path.join(root,filename)
  42.                         self.trimFile(filepath)
  43.                         #print filepath
  44.  
  45.     def run(self,argv):
  46.         self.basePath = os.path.normpath(argv[1])
  47.         if len(argv) < 3:
  48.             self.trimExtList.append('java')
  49.         else:
  50.             for i in range(len(argv)-2):
  51.                 self.trimExtList.append(argv[2+i])
  52.         self.showMessages()
  53.         self.getFileList(argv[1])
  54.  
  55. if __name__ == '__main__':
  56.         if len(sys.argv) < 2:
  57.             print 'USEAGE:python %s dirName [ext eg:java php cpp]' % __file__
  58.             sys.exit(0)
  59.        
  60.         tObj = RemoveBom()
  61.         tObj.run(sys.argv)
  62.  
  63. #//python/1158

回复 "批量去除UTF8的Bom标签"

这儿你可以回复上面这条便签

captcha