当前位置:网站首页>4. File modification

4. File modification

2022-07-06 02:43:00 wwanxinghao

General methods for modifying the contents of documents :

Rewrite the data into a new file , Then delete the old file , Then change the copy to the main version

The following is a general's surname “ Zhou ” Replace with last name :“ Yang ” The operation of

The code is as follows :

import os

with open(' name .txt',mode='r',encoding='utf-8') as f1,\
    open(' name - copy .txt',mode='w',encoding='utf-8') as f2:
    for i in f1:
        if i.startswith(' Zhou '):
            i = i.replace(' Zhou ',' Yang ')
        f2.write(i)

os.remove(' name .txt') #  Delete the original document 
os.rename(' name - copy .txt',' name .txt')  #  Rename the copy file 

 

原网站

版权声明
本文为[wwanxinghao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140002488019.html