1. 程式人生 > >python文件內容修改1

python文件內容修改1

pre path ace nbsp color 修改 class alt span

#!/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@")

python文件內容修改1