Skip to content

Instantly share code, notes, and snippets.

@zhaofeng-shu33
Last active May 18, 2020 08:44
Show Gist options
  • Save zhaofeng-shu33/2c008b01e3d5104faa0817ab05f39e1c to your computer and use it in GitHub Desktop.
Save zhaofeng-shu33/2c008b01e3d5104faa0817ab05f39e1c to your computer and use it in GitHub Desktop.
import os
import argparse
def convert(filename):
with open(filename, 'rb') as f:
st = f.read()
try:
st = st.decode('gbk')
except Exception as e:
print('gbk decode error for ' + filename)
return
with open(filename, 'wb') as f:
f.write(st.encode('utf-8'))
if filename.find(' ') > 0:
os.rename(filename, filename.replace(' ', '_'))
def parse_all(ext):
for i in os.listdir('./'):
if i.find('.' + ext) > 0:
convert(i)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--type', default='txt')
args = parser.parse_args()
parse_all(args.type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment