国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現批量轉換文件編碼的方法

2019-11-25 17:07:08
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現批量轉換文件編碼的方法。分享給大家供大家參考。具體如下:

這里將某個目錄下的所有文件從一種編碼轉換為另一種編碼,然后保存

import osimport shutildef match(config,fullpath,type):  flag=False  if type == 'exclude':    for item in config['src']['exclude']:      if fullpath.startswith(config['src']['path']+os.path.sep+item):        flag=True        break  if type=='filter':    for item in config['src']['filter']:      if fullpath.endswith(item):        flag=True        break  return flagdef conver_file(param):  for root, dirs, files in os.walk(param['src']['path']):    for filename in files:      readfile=root+os.path.sep+"%s" %filename      print(readfile)      if 'filter' in param['src']:        if not (match(param,readfile,'filter')):          continue      s=''      outfile=readfile.replace(param['src']['path'],param['dest']['path'])      try :        s=open(readfile,encoding=param['src']['encoding']).read()      except:        print("file %s read erro" % readfile)        shutil.copy(readfile,outfile)      if s: #False and        print("save")        with open(outfile, mode='w', encoding=param['dest']['encoding']) as a_file:          a_file.write(s)    for dirname in dirs:      file=root+os.path.sep+"%s" %dirname      if 'exclude' in param['src']:        if(match(param,file,'exclude')):          continue      outdir=file.replace(param['src']['path'],param['dest']['path'])      #print(outdir)      if not os.path.isdir(outdir):        os.mkdir(outdir)if __name__ == "__main__":  param={'src':{'path':r'D:/work/test/trunk','encoding':'gbk','exclude':['dataa'],'filter':['.php','.html','.htm']},    'dest':{'path':"f://test//new",'encoding':'utf-8'}}  conver_file(param)

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 从化市| 信宜市| 德阳市| 葫芦岛市| 黄大仙区| 荆州市| 乌鲁木齐县| 驻马店市| 杨浦区| 石渠县| 岚皋县| 武功县| 辉南县| 太谷县| 泽普县| 闸北区| 托克托县| 兰溪市| 贡嘎县| 福海县| 射阳县| 巴林右旗| 阜康市| 靖边县| 山丹县| 蒙自县| 麻城市| 景德镇市| 安远县| 隆林| 潼关县| 枣阳市| 泸水县| 宽城| 兴宁市| 麻江县| 连山| 岐山县| 朝阳市| 富平县| 兰考县|