-
Notifications
You must be signed in to change notification settings - Fork 39
/
convertToUtf8.py
executable file
·52 lines (45 loc) · 1.33 KB
/
convertToUtf8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/env python
import sys
import os
import traceback
CHARSETS = ("ascii", "cp1250", "cp1252", "iso-8859-9", "iso-8859-15")
def is_utf8(filepath):
try:
file = open(filepath, "rb")
file.read().decode('utf-8')
file.close()
return True
except:
return False
def to_utf8(path):
for charset in CHARSETS:
try:
f = open(path, 'rb')
content = f.read().decode(charset)
f.close()
f = open(path, 'wb')
f.write(content.encode('utf-8'))
f.close()
return "Converting to utf-8: " + os.path.split(path)[1]
except:
pass
return "Unable to open " + os.path.split(path)[1] + " - unknown charset or binary file."
def run():
message = ""
for filename in sys.argv[1:]:
if os.path.isfile(filename):
if is_utf8(filename):
message += os.path.split(filename)[1] + " is already in utf-8.\n"
else:
message += to_utf8(filename) + "\n"
return message.strip()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: " + sys.argv[0] + " file1 [file2 ...]")
sys.exit(1)
try:
message = run()
except:
message = traceback.format_exc()
if message != "":
print(message)