import os, sys, string
encoder = 'cp874'
decoder = 'utf8'
def replace_invalid_char(line,utf_char,tis_char):
return line.replace(utf_char,tis_char)
def convertline(line):
# CHECK INVALID CHAR
# line=replace_invalid_char(line,"\xe2\x80\x98","'")
# line=replace_invalid_char(line,"\xe2\x80\x99","'")
# line=replace_invalid_char(line,"\xe2\x80\x9c",'"')
# line=replace_invalid_char(line,"\xe2\x80\x9d",'"')
# line=replace_invalid_char(line,"\xe2\x80\xa6","...")
# line=replace_invalid_char(line,"\xef\x9c\x8f","\xe0\xb8\x8d") #YOR YING
# line=replace_invalid_char(line,"\xef\x9c\x9a","\xe0\xb8\xba") #PINTU
# line=replace_invalid_char(line,"\xe2\x80\x93","-")
# line=replace_invalid_char(line,"\xef\x82\xae","->")
# line=replace_invalid_char(line,"\xef\xa3\x82","?") # UNKNOWN
# line=replace_invalid_char(line,"\xef\xa3\x83","?") # UNKNOWN
return line.decode(decoder).encode(encoder)
f = open('error.txt', 'w')
for root, dirs, files in os.walk('.') :
for ent in files + dirs :
if ent == '.' or ent == '..' :
continue
try :
new_name = convertline(ent)
except :
f.write('convert:' + os.path.join(root, ent) + '\n')
continue
if new_name != ent :
src = os.path.join(root, ent)
dest = os.path.join(root, new_name)
#print 'renaming ' + src + ' -> ' + dest
try :
os.rename(src, dest)
except :
f.write('permission:' + src + '->' + dest + '\n')
f.close()
เอาบางส่วนของโค้ดมาจาก http://www.thaitux.info/node/115 ครับ