python bulk modification file encoding format method
- 2020-11-03 22:30:50
- OfStack
This article shares the specific code of python to modify the file encoding format in batches for your reference. The specific content is as follows
Instructions for use:
1. Tools: Python2.7.6+chardet2.3.0, chardet2.3.0 Download address: Click here
2. Environment configuration: Python installation + configuration environment variable, chardet unzip under Python installation directory \Lib\ ES16en-ES17en
Example: Batch modify the encoding format of all.cpp files under the current path as UTF-8, the code is as follows:
python:
import os
import sys
import codecs
import chardet
def convert(filename,out_enc="UTF-8"):
try:
content=codecs.open(filename,'r').read()
source_encoding=chardet.detect(content)['encoding']
print source_encoding
content=content.decode(source_encoding).encode(out_enc)
codecs.open(filename,'w').write(content)
except IOError as err:
print("I/O error:{0}".format(err))
def explore(dir):
for root,dirs,files in os.walk(dir):
for file in files:
if os.path.splitext(file)[1]=='.cpp':
print file
path=os.path.join(root,file)
convert(path)
def main():
explore(os.getcwd())
if __name__=="__main__":
main()