python文件内容修改

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
def alter(file,old_str,new_str):
    file_data = ""
    with open(file, "r", ) as f:
        for line in f:
            if old_str in line:
                line = line.replace(old_str,new_str)
            file_data += line
    with open(file,"w",) as f:
        f.write(file_data)

alter("@option.file_path@", "@option.old_file@", "@option.new_file@")

猜你喜欢

转载自www.cnblogs.com/niwajiang1/p/9419048.html
今日推荐