Skip to content

Instantly share code, notes, and snippets.

@xuecan
Last active December 23, 2015 09:18
Show Gist options
  • Select an option

  • Save xuecan/e0e2dd45bdbc7e17a129 to your computer and use it in GitHub Desktop.

Select an option

Save xuecan/e0e2dd45bdbc7e17a129 to your computer and use it in GitHub Desktop.
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