convert.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import os
  2. import chardet
  3. import codecs
  4. import logging
  5. import argparse
  6. class ConvertCode(object):
  7. '''convert file encode'''
  8. def __init__(self, debug=False):
  9. super(ConvertCode, self).__init__()
  10. parser = argparse.ArgumentParser(
  11. description='Convert file encode to utf8.')
  12. parser.add_argument(
  13. "--dir", help="set a file directory to convert, eg: /workspace. default: ./", type=str, default="./")
  14. parser.add_argument(
  15. "--suffix", help="set the file suffix to convert, eg: *.java. default: *.*", type=str, default="*.*")
  16. args = parser.parse_args()
  17. if args.dir:
  18. self.dir = args.dir
  19. if args.suffix:
  20. self.suffix = args.suffix
  21. if debug == True:
  22. logging.basicConfig(level=logging.DEBUG)
  23. @staticmethod
  24. def WriteFile(filePath, u, encoding="utf-8"):
  25. with codecs.open(filePath, "w", encoding) as f:
  26. f.write(u)
  27. @staticmethod
  28. def GBK_2_UTF8(src, dst):
  29. ''' 检测编码,coding可能检测不到编码,有异常 '''
  30. f = open(src, "rb")
  31. coding = chardet.detect(f.read())["encoding"]
  32. f.close()
  33. if coding != "utf-8":
  34. with codecs.open(src, "r", coding) as f:
  35. try:
  36. ConvertCode.WriteFile(dst, f.read(), encoding="utf-8")
  37. try:
  38. print(src + " " + coding + " to utf-8 converted!")
  39. except Exception:
  40. print("print error")
  41. except Exception:
  42. print(src + " " + coding + " read error")
  43. def run(self):
  44. ''' 把目录中的*.java编码由gbk转换为utf-8 '''
  45. for parent, dirnames, filenames in os.walk(self.dir):
  46. for dirname in dirnames:
  47. # 递归函数,遍历所有子文件夹
  48. self.run(dirname)
  49. for filename in filenames:
  50. if self.suffix=="*.*":
  51. pass
  52. elif filename.endswith(".java"):
  53. ConvertCode.GBK_2_UTF8(os.path.join(parent, filename),
  54. os.path.join(parent, filename))
  55. if __name__ == "__main__":
  56. converter = ConvertCode()
  57. converter.run()