python實(shí)現(xiàn)文件名批量替換和內(nèi)容替換
指定文件夾,指定文件類(lèi)型,替換該文件夾下全部文件的內(nèi)容。
注意在window下的讀寫(xiě)內(nèi)容需要指定編碼,還需要在文件頭指定#coding:utf-8 編碼,避免出現(xiàn)編碼問(wèn)題。
#coding:utf-8
import os
import os.path
path='.'
oldStr='.php'
newStr='.html'
for (dirpath, dirnames, filenames) in os.walk(path):
for file in filenames:
if os.path.splitext(file)[1]=='.html':
print(file)
filepath=os.path.join(dirpath,file)
try:
text_file = open(filepath, "r")
lines = text_file.readlines()
text_file.close()
output = open(filepath,'w',encoding= 'utf-8')
for line in lines:
#print(line)
if not line:
break
if(oldStr in line):
tmp = line.split(oldStr)
temp = tmp[0] + newStr + tmp[1]
output.write(temp)
else:
output.write(line)
output.close()
except Exception:
print(Exception)
break
這個(gè)示例可以批量替換文件名和內(nèi)容
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, re
def multi_replace(text, adict):
rx = re.compile('|'.join(map(re.escape, adict)))
def xlat(match):
return adict[match.group(0)]
return rx.sub(xlat, text)
def batrename(curdir, pairs):
for fn in os.listdir(curdir):
newfn = multi_replace(fn, pairs)
if newfn != fn:
print("Renames %s to %s in %s." % (fn, newfn, curdir))
os.rename(os.path.join(curdir, fn), os.path.join(curdir, newfn))
file = os.path.join(curdir, newfn)
if os.path.isdir(file):
batrename(file, pairs)
continue
text = open(file).read()
newtext = multi_replace(text, pairs)
if newtext != text:
print("Renames %s." % (file,))
open(file, 'w').write(newtext)
if __name__=="__main__":
while True:
oldname = raw_input("Old name: ")
newname = raw_input("New name: ")
if oldname and newname:
batrename(os.path.abspath('.'), {oldname:newname})
else: break
相關(guān)文章
Python使用GeekConcurrent實(shí)現(xiàn)量化編程
這篇文章主要為大家詳細(xì)介紹了Python中的協(xié)程并發(fā)編程以及如何使用GeekConcurrent庫(kù)來(lái)實(shí)現(xiàn)面向量化編程,感興趣的小伙伴可以了解一下2025-02-02
Python實(shí)現(xiàn)的購(gòu)物車(chē)功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的購(gòu)物車(chē)功能,涉及Python通過(guò)交互與數(shù)值運(yùn)算實(shí)現(xiàn)購(gòu)物車(chē)功能的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Python lambda和Python def區(qū)別分析
Python支持一種有趣的語(yǔ)法,它允許你快速定義單行的最小函數(shù)。這些叫做lambda的函數(shù),是從Lisp借用來(lái)的,可以用在任何需要函數(shù)的地方2014-11-11
淺談numpy中l(wèi)inspace的用法 (等差數(shù)列創(chuàng)建函數(shù))
下面小編就為大家?guī)?lái)一篇淺談numpy中l(wèi)inspace的用法 (等差數(shù)列創(chuàng)建函數(shù))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼
這篇文章主要介紹了Python Opencv實(shí)現(xiàn)單目標(biāo)檢測(cè)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python?sklearn轉(zhuǎn)換器估計(jì)器和K-近鄰算法
這篇文章主要介紹了Python?sklearn轉(zhuǎn)換器估計(jì)器和K-近鄰算法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Python庫(kù)中可以操作JavaScript盤(pán)點(diǎn)解析
這篇文章主要為大家介紹了Python庫(kù)之可以操作JavaScript盤(pán)點(diǎn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python利用docx模塊實(shí)現(xiàn)快速操作word文件
這篇文章主要為大家詳細(xì)介紹了Python如何利用docx模塊實(shí)現(xiàn)快速操作word文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09

