同时打开多个文件,按行修改文件内容
with open('test.py', 'r', encoding='utf8') as fr, \
        open('test_swap.py', 'w', encoding='utf8') as fw:
        
    # 再大的文件都能修改
    for i in fr:
        s = '傻逼'
        i = i.replace('sb', s)
        fw.write(i)
        
        fw.flush()  # 先保存成功再继续运行

import os

os.remove('test.py')
os.rename('test_swap.py', 'test.py')