#-*-Coding:utf-8-*-
__author__ = ' Walkskyer '
Import OS
Import Glob
Class Encoding:
def __init__ (self):
#文件扩展名
Self.ext = ". *"
#编码
Self.srcencoding=none
Self.dstencoding=none
def convertencoding (self, content, Srcencoding=none, Dstencoding=none):
Return Content.decode (self.srcencoding). Encode (self.dstencoding)
def processdirectory (self, args, dirname, filenames):
print ' Directory ', dirname
for filename in filenames:
& nbsp; if not os.path.isdir (dirname+ '/' +filename):
If Filename.endswith ( Self.ext) or Self.ext = = ". *":
print ' File ', filename
self.f2f (dirname+ '/' +filename)
def f2f (self, filepath, Srcencoding=none, Dstencoding=none):
try:
f1 = open (filepath, ' RB ')
temp = f1.read ()
f1.close ()
F2 = open (filepath, ' WB ')
F2.write (Temp.decode (self.srcencoding). Encode (self.dstencoding))
f2.close ()
Print Transcoding succeeded '
except Exception, E:
Print e
def colectfiletype (self, dirname, FileType):
for filename in Glob.glob (R ' *. ') +filetype):
Print filename
def setext (Self, ext):
If not ext.startswith ('. '):
ext = "." + Ext
Self.ext = Ext
def setsrc (self, Encoding):
Self.srcencoding=encoding
def setdst (self, Encoding):
Self.dstencoding=encoding
if __name__ = = ' __main__ ':
obj = Encoding ()
Print U ' Please enter file type: '
Obj.setext (Raw_input ())
Print u ' please input file original encoding: '
OBJ.SETSRC (Raw_input ())
Print U ' Please enter file target type: '
OBJ.SETDST (Raw_input ())
"" "Obj.setext (' HTML ')
OBJ.SETSRC (' GBK ')
OBJ.SETDST (' utf-8 ') "" "
Print U ' Please enter the directory where the file is located: '
Path = Raw_input ()
Os.path.walk (Path, obj.processdirectory, None)