1
1

convert_ui.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import chardet
  3. import codecs
  4. def WriteFile(filePath, u, encoding="utf-8"):
  5. with codecs.open(filePath, "w", encoding) as f:
  6. f.write(u)
  7. def GBK_2_UTF8(src, dst):
  8. # 检测编码,coding可能检测不到编码,有异常
  9. f = open(src, "rb")
  10. coding = chardet.detect(f.read())["encoding"]
  11. f.close()
  12. if coding != "utf-8":
  13. with codecs.open(src, "r", coding) as f:
  14. try:
  15. WriteFile(dst, f.read(), encoding="utf-8")
  16. try:
  17. print(src + " " + coding + " to utf-8 converted!")
  18. except Exception:
  19. print("print error")
  20. except Exception:
  21. print(src +" "+ coding+ " read error")
  22. # 把目录中的*.bas编码由gbk转换为utf-8
  23. def ReadDirectoryFile(rootdir):
  24. for parent, dirnames, filenames in os.walk(rootdir):
  25. for dirname in dirnames:
  26. #递归函数,遍历所有子文件夹
  27. ReadDirectoryFile(dirname)
  28. for filename in filenames:
  29. if filename.endswith(".frm"):
  30. GBK_2_UTF8(os.path.join(parent, filename),
  31. os.path.join(parent, filename))
  32. if __name__ == "__main__":
  33. src_path = "C:/Soft/Git/srgb/corelvba/UI"
  34. ReadDirectoryFile(src_path)