程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Batch modification of Python file content

編輯:Python
""" Write function , The user passes in the modified file name , And the content to be modified , Execute function , Complete the batch modification of the whole file Put... In the file You are a Change to Ha ha ha oldfile The contents of the document : You are a , Stretch your eyes , When spring comes . Aim far , Read often . Be happy to make friends , Cherish the four seasons , You are a . The mountain is high and there is a road , You are a , There is a boat crossing the water . You are a , Harmony makes Chunyan , New year is better than old year . """
import os
def reFile(file, old_connet, new_connet):
with open(file, encoding='utf-8', mode='r') as f, \
open(file + '.bak', encoding='utf-8', mode='a') as ff:
for line in f:
connet = line.replace(old_connet, new_connet)
ff.write(connet)
os.remove(file)
# File rename 
os.rename(file + '.bak', file)
reFile('oldFile', ' You are a ', ' Ha ha ha ')

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved