做平台的网站有哪些内容,网站优化公司哪个好,400个成品短视频,做私人没有备案的网站本文实例为大家分享了python批量修改文件编码格式的具体代码#xff0c;供大家参考#xff0c;具体内容如下
使用说明#xff1a;
1、使用工具#xff1a;Python2.7.6chardet2.3.0#xff0c;chardet2.3.0下载地址#xff1a;点击这里
2、环境配置#xff1a;Python安装…本文实例为大家分享了python批量修改文件编码格式的具体代码供大家参考具体内容如下
使用说明
1、使用工具Python2.7.6chardet2.3.0chardet2.3.0下载地址点击这里
2、环境配置Python安装配置环境变量chardet解压放在Python安装目录\Lib\site-packages下
举例批量修改当前路径下所有.cpp文件的编码格式为UTF-8代码如下
python
import os
import sys
import codecs
import chardet
def convert(filename,out_encUTF-8):
try:
contentcodecs.open(filename,r).read()
source_encodingchardet.detect(content)[encoding]
print source_encoding
contentcontent.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
pathos.path.join(root,file)
convert(path)
def main():
explore(os.getcwd())
if __name____main__:
main()
以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持脚本之家。