導航:首頁 > 文件目錄 > python替換文件內容

python替換文件內容

發布時間:2023-09-12 11:09:05

『壹』 Python批量修改文本文件內容的方法

Python批量修改文本文復件制內容的方法
Python批量替換文件內容,支持嵌套文件夾
import os
path="./"
for root,dirs,files in os.walk(path):
for name in files:
#print name
if name.endswith(".html"):
#print root,dirs,name
filename=root+"/"+name
f=open(filename,"r")
filecontent=""
line=f.readline()
while line:
l=line.replace(":/arcgis_js_api","/arcgisapi")
filecontent=filecontent+l
line=f.readline()
f.close()
f=file(filename,"w")
f.writelines(filecontent)
f.close()
關於本文給大家介紹的Python批量修改文本文件內容的方法

『貳』 Python如何將文件夾中的所有txt文件的內容替換

很直接簡單的方法就是,遍歷文件夾下所有的txt文件,然後讀取內容,把內容中的","替換成空格,然後重新寫入這個文件,這樣就可以了。

『叄』 python里怎樣替換,修改文本內容

當我們讀取文件中內容後,如果想要修改文件中的某一行或者某一個位置的內容,在python中是沒有辦法直接實現的,如果想要實現這樣的操作只能先把文件所有的內容全部讀取出來,然後進行匹配修改後寫入到新的文件中。

實例代碼如下所示:

備註:

1. 舊文件的內容

hello,world
yanyan is good girl
Good day is good day

2. 新文件在代碼執行後的內容

hello,world
yanyan is good girl
hello,yanyan

3. 需要注意的是許可權的問題,對於舊文件必須要有讀取許可權,對於新的文件必須要有寫入許可權

『肆』 gitpython如何修改文件內容不影響格式

三種方法

_弧⑿薷腦募絞?

_ef alter(file,old_str,new_str):

?

?"""

_婊晃募械淖址?

?:param file:文件名

?:param old_str:就字元串

?:param new_str:新字元串

?:return:

?

?"""

_ile_data = ""

?

_ith open(file, "r", encoding="utf-8") as f:

?

_or line in f:

?

_f old_str in line:

?

_ine = line.replace(old_str,new_str)

?

_ile_data += line

?

_ith open(file,"w",encoding="utf-8") as f:

?

_.write(file_data)

?

_lter("file1", "09876", "python")

?

__言募諶鶯鴕薷牡哪諶菪吹叫攣募薪寫媧⒌姆絞?

?

?2.1 python字元串替換的方法,修改文件內容

?

_mport os

?

_ef alter(file,old_str,new_str):

?

?"""

?

_婊壞淖址吹揭桓魴碌奈募校緩蠼募境攣募奈次募拿?

?

?:param file: 文件路徑

?

?:param old_str: 需要替換的字元串

?

?:param new_str: 替換的字元串

?

?:return: None

?

?"""

?

_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:

?

_or line in f1:

?

_f old_str in line:

?

_ine = line.replace(old_str, new_str)

?

_2.write(line)

?

_s.remove(file)

?

_s.rename("%s.bak" % file, file)

?

_lter("file1", "python", "測試")

?

?2.2 python 使用正則表達式 替換文件內容 re.sub 方法替換

?

_mport re,os

?

_ef alter(file,old_str,new_str):

?

_ith open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:

?

_or line in f1:

?

_2.write(re.sub(old_str,new_str,line))

?

_s.remove(file)

?

_s.rename("%s.bak" % file, file)

『伍』 Python如何將文件夾中的所有txt文件的內容替換

下面是我寫的,供參考:

import os

path = r'D:Desktope'

files = list(filter(lambda file:file[-4:]=='.txt',os.listdir(path)))

for file in files:

with open(path+os.sep+file,'r+') as f:

data = f.read()

data.replace('wo','我')

f.write(data)

『陸』 python文本內容替換

這樣編寫:
fa=open("A.txt","r")
ta=fa.readlines()
fb=open("B.txt","r")
tb=fb.readlines()
tb[2:-9]=ta
fa.close()
fb.close()
fb=open("B.txt","w")
fb.writelines(tb)
fb.close()

閱讀全文

與python替換文件內容相關的資料

熱點內容
怎麼把圖片加入視頻文件夾 瀏覽:734
滑鼠右鍵的壓縮文件 瀏覽:44
awr導出cad文件 瀏覽:925
參公文件去哪裡找 瀏覽:827
excel表批量日期設置成文件夾存放 瀏覽:90
如何把資料庫加入其中 瀏覽:661
編程除法怎麼取消取整 瀏覽:625
股票編程軟體哪裡有賣 瀏覽:503
access導入多個txt文件 瀏覽:917
大臉app安卓下載 瀏覽:439
怎麼休改文件名 瀏覽:989
cdr導出圖片不顯示文件名 瀏覽:761
pcdmis如何離線編程 瀏覽:201
微信推文插入文件 瀏覽:844
生產文件櫃圖片 瀏覽:244
如何設置微信數據增加 瀏覽:95
excel文件不再增大 瀏覽:613
ug編程刀路怎麼復制不用滑鼠選取 瀏覽:97
excel文件打開擴展名錯誤 瀏覽:10
阿里巴巴怎麼做數據包 瀏覽:442

友情鏈接