Skip to content

Instantly share code, notes, and snippets.

@GZShi
Created March 18, 2014 07:40
Show Gist options
  • Save GZShi/9615279 to your computer and use it in GitHub Desktop.
Save GZShi/9615279 to your computer and use it in GitHub Desktop.
批量GBK转UTF8
import os, sys
def convert(filepath, in_encode = "GBK", out_encode = "UTF8"):
try:
in_content = open(filepath).read()
out_content = in_content.decode(in_encode).encode(out_encode)
open(filepath, 'w').write(out_content)
print "[OK] Convert " + filepath
except:
print "[ERROR] Convert " + filepath
def explore(dir):
for root, dirs, files in os.walk(dir):
for file in files:
path = os.path.join(root, file)
convert(path)
def main():
for path in sys.argv[1:]:
if os.path.isfile(path):
print "is file"
convert(path)
elif os.path.isdir(path):
print "is dir"
explore(path)
# usage:
# convert.py ./
# convert.py ./gbkfile.txt
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment