""" 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 ')