Last active
December 23, 2015 09:18
-
-
Save xuecan/e0e2dd45bdbc7e17a129 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| def walk(r='.'): | |
| for cd,_,fns in os.walk(r): | |
| for fn in fns: | |
| yield os.path.join(cd,fn) | |
| def check(fn,enc='utf8'): | |
| try: | |
| with open(fn,'r',encoding=enc) as f: | |
| c = f.read() | |
| except Exception: | |
| return fn | |
| def convert(fn,enc0='gbk',enc1='utf8'): | |
| with open(fn,'r',encoding=enc0) as f: | |
| c = f.read() | |
| with open(fn,'w',encoding=enc1) as f: | |
| f.write(c) | |
| print(fn) | |
| for fn in walk(): | |
| if check(fn): | |
| convert(fn) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment