Python實(shí)現(xiàn)批量轉(zhuǎn)換文件編碼的方法
本文實(shí)例講述了Python實(shí)現(xiàn)批量轉(zhuǎn)換文件編碼的方法。分享給大家供大家參考。具體如下:
這里將某個(gè)目錄下的所有文件從一種編碼轉(zhuǎn)換為另一種編碼,然后保存
import os import shutil def 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 flag def 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)
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python中靜態(tài)方法,類(lèi)方法,屬性方法使用方法
這篇文章主要介紹了Python中靜態(tài)方法,類(lèi)方法,屬性方法使用方法,文章圍繞主題的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Django開(kāi)發(fā)中使用Ueditor上傳圖片遇到的坑及解決
在Django開(kāi)發(fā)中使用Ueditor上傳圖片時(shí),可能會(huì)遇到后端配置不正確的問(wèn)題,建議在實(shí)例化Ueditor后加上serverUrl,這可以在Chrome的F12工具中查看請(qǐng)求的后端配置項(xiàng),此外,如果需要修改上傳路徑,可以在配置文件中更改路徑,并調(diào)整view.py中的代碼來(lái)管理上傳文件2024-09-09python 虛擬環(huán)境的創(chuàng)建與使用方法
本文先介紹虛擬環(huán)境的基礎(chǔ)知識(shí)以及使用方法,然后再深入介紹虛擬環(huán)境背后的工作原理,需要的朋友可以參考下2021-06-06Python通過(guò)Pillow實(shí)現(xiàn)圖片對(duì)比
這篇文章主要介紹了Python Pillow實(shí)現(xiàn)圖片對(duì)比,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python從csv文件中讀取數(shù)據(jù)及提取數(shù)據(jù)的方法
這篇文章主要介紹了Python從csv文件中讀取數(shù)據(jù)并提取數(shù)據(jù)的方法,文中通過(guò)多種方法給大家講解獲取指定列的數(shù)據(jù),并存入一個(gè)數(shù)組中,每種方法通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11python區(qū)塊及區(qū)塊鏈的開(kāi)發(fā)詳解
這篇文章主要介紹了python區(qū)塊及區(qū)塊鏈的開(kāi)發(fā)詳解的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07深入理解Pytorch微調(diào)torchvision模型
PyTorch是一個(gè)基于Torch的Python開(kāi)源機(jī)器學(xué)習(xí)庫(kù),用于自然語(yǔ)言處理等應(yīng)用程序。它主要由Facebookd的人工智能小組開(kāi)發(fā),不僅能夠 實(shí)現(xiàn)強(qiáng)大的GPU加速,同時(shí)還支持動(dòng)態(tài)神經(jīng)網(wǎng)絡(luò),這一點(diǎn)是現(xiàn)在很多主流框架如TensorFlow都不支持的2021-11-11