-
Notifications
You must be signed in to change notification settings - Fork 2
/
sjis2utf8.py
111 lines (101 loc) · 3.36 KB
/
sjis2utf8.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# The MIT License (MIT)
#
# Copyright (c) <year> <copyright holders>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
###############################################################################
# from __future__ import unicode_literals
from __future__ import division
import os
import sys
import codecs
def ShiftjisToUTF8_f(folder):
cwd = os.path.abspath(os.getcwd())
os.chdir(os.path.abspath(folder))
print(folder)
print('-' * 80)
for item in os.listdir('.'):
fullname = item
try:
if sys.version_info.major >= 3:
f_old = str.encode(item).decode('utf8')
f_new = str.encode(item).decode('utf8').encode('mbcs').decode('shiftjis')
else:
f_old = b'%s' % item
f_new = f_old.decode('shiftjis')
try:
f_new.encode('gbk')
os.rename(f_old, f_new)
except:
f_new = f_old
pass
fullname = f_new
if sys.version_info.major >= 3:
print('[%s] ==> [%s]' % (f_old, f_new))
else:
print(u'[%s] ==> [%s]' % (f_old.decode('gbk'), f_new))
except:
pass
if os.path.isdir(fullname):
ShiftjisToUTF8_f(fullname)
print('=' * 80)
# os.chdir('..')
os.chdir(cwd)
def ShiftjisToUTF8_ft(folder):
cwd = os.path.abspath(os.getcwd())
os.chdir(os.path.abspath(folder))
print(folder)
print('-' * 80)
for item in os.listdir('.'):
fullname = item
if sys.version_info.major >= 3:
f_old = str.encode(item).decode('utf8')
f_new = str.encode(item).decode('utf8').encode('mbcs').decode('shiftjis')
else:
f_old = b'%s' % item
f_new = f_old.decode('shiftjis')
os.rename(f_old, f_new)
fullname = f_new
if sys.version_info.major >= 3:
print('[%s] ==> [%s]' % (f_old, f_new))
else:
print(u'[%s] ==> [%s]' % (f_old.decode('gbk'), f_new))
if os.path.isdir(fullname):
ShiftjisToUTF8_ft(fullname)
print('=' * 80)
# os.chdir('..')
os.chdir(cwd)
def ShiftjisToUTF8(name):
try:
f_old = b'%s' % name
f_new = f_old.decode('shiftjis')
os.rename(f_old, f_new)
print(u'[%s] ==> [%s]' % (f_old.decode('gbk'), f_new))
return(f_new)
except:
return(f_old)
pass
if __name__ == '__main__':
ROOT = '.'
if len(sys.argv) >= 2:
ROOT = sys.argv[1]
ShiftjisToUTF8_f(ROOT)