How to modify a file in Python

The original contents of the file and write the way you want to modify the contents of the new file storing

import os
def alter(file, old_str, new_str):
    '''
    将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原文件的名字
    :param file:文件路径
    :param old_str:需要替换的字符串
    :param new_str:替换的字符串
    :return: None
    '''
    with open(file, 'r', encoding='utf-8') as f1,\
            open('%s.bak'%file, 'w', encoding='utf-8') as f2:
        for line in f1:
            if old_str in line:
            line = line.replace(old_str, new_str)
        f2.write(line)
    os.remove(file)
    os.rename('%s.bak'%file, file)
    

Guess you like

Origin www.cnblogs.com/17vv/p/11272574.html