The
is ideal for converting old projects, or downloading the source code for the GBK project from the Internet
, which was found online, combined with the following:
#-*-coding:utf-8-*-Import codecs import OS import shutil import re import sys def convert (filename, In_enc = "GBK", Out_enc= "UTF-8"): shutil.copyfile (filename, filename + '. Bak ') # read the File content = open (filename). R EAD () # convert the concent try:new_content = Content.decode (In_enc). Encode (Out_enc) #write t o File open (filename, ' W '). Write (new_content) except:print "error ... "Def Explore (dir): for Root, dirs, files in Os.walk (dir): For file in Files:if File.lower ()
. EndsWith ('. Java '): Path = Os.path.join (root, file) print "Convert" + path, Convert (path) print "Done" def process_bak_files (action= ' Restore '): For root, dirs, files in Os.walk (OS.GETCWD ()): For F in Files:if F.lower (). EndsWith ('. Java.bak '): Source = os. Path.join (root, f) target = Os.path. Join (Root, Re.sub (' \.java\.bak$ ', '. Java ', F, flags=re). IGNORECASE) try:if action = ' Restore ': shutil.move (source, Target) elif action = = ' Clear ': os.remove (source) except Excep
tion, E:print source def main (): If Len (sys.argv) > 1:path = sys.argv[1] If Os.path.isfile (path): Convert (path) elif Os.path.isdir (path): Explore (path) if __name__ = = "__main__": #process_bak_files (action= ' Clear ') main ()
Usage
Python encode.py [path]