Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Fixes MSSQL scripts encoding in UTC-2 (UTF-16) to be UTF-8.
  5. """
  6.  
  7. import os
  8.  
  9. SCRIPTS_DIR = os.path.expanduser("~/Projects/scripts")
  10.  
  11.  
  12. def read_file(file, encoding='utf-16'):
  13. if not os.path.exists(file):
  14. raise Exception("File does not exist: {}".format(file))
  15.  
  16. with open(file, 'r', encoding=encoding) as fh:
  17. return fh.read()
  18.  
  19.  
  20. def write_file(file, contents, encoding='utf-8'):
  21. with open(file, 'w', encoding=encoding) as fh:
  22. return fh.write(contents)
  23.  
  24.  
  25. def main():
  26. sql_files = []
  27. for root, dirs, files in os.walk(SCRIPTS_DIR):
  28. for file in [f for f in files if f.endswith(".sql")]:
  29. sql_files.append(os.path.join(root, file))
  30.  
  31. for file in sql_files:
  32. contents = read_file(file, encoding='utf-16')
  33. write_file(file, contents, encoding='utf-8')
  34. print(file)
  35.  
  36.  
  37. if __name__ == '__main__':
  38. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement