Created
March 18, 2014 07:40
-
-
Save GZShi/9615279 to your computer and use it in GitHub Desktop.
批量GBK转UTF8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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