from typeguard import typechecked @typechecked def replace_str_in_fil(old_fil: str, new_fil: str, tgt: str, repl: str): """ old_fil: old file. tgt: str to be replaced. repl: replacement. new_fil: upd'ed file. Replaces tgt in old file. Saves upd'ed file. """ # read old file with open(old_fil, "r") as f: lines = f.read() # replace target str new_lines = lines.replace(tgt, repl) # save upd'ed file with open(new_fil, "w") as f: f.write(new_lines)