-
Notifications
You must be signed in to change notification settings - Fork 27
/
fixgbknames.py
32 lines (28 loc) · 976 Bytes
/
fixgbknames.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
"""
If you have a bunch of files in Linux system whose filename is encoded with gbk,
you will find `ls` cannot correctly display them,
because filenames should be encoded with utf-8 under Linux.
So, let's change the filename to correct encoding 'utf-8'
This script can only work under Python3
"""
try:
import http #this is only for testing if you're using py3
except:
print("This script can only work under Python3")
exit(1)
import os
ok=0
notok = 0
for root, dirs, files in os.walk(b"."):
for filename in files:
filename = os.path.join(root, filename).replace(b'`',b'\\`')
try:
filename.decode('utf-8')
ok += 1
except:
print(filename)
notok += 1
command = b'mv "'+filename+b'" "'+filename.decode('gbk').encode('utf-8')+b'"'
os.system(command)
print("changed {} files".format(notok))
print("leave {} files untouched".format(ok))