(file, encoding, old, new)
| 11 | |
| 12 | |
| 13 | def text_file_replace(file, encoding, old, new): |
| 14 | lines = [] |
| 15 | cnt = 0 |
| 16 | with open(file=file, mode="r", encoding=encoding) as fd: |
| 17 | for line in fd: |
| 18 | cnt += line.count(old) |
| 19 | lines.append(line.replace(old, new)) |
| 20 | with open(file=file, mode="w", encoding=encoding) as fd: |
| 21 | fd.writelines(lines) |
| 22 | print('{} occurence(s) of "{}" have been replaced with "{}"'.format(cnt, old, new)) |
| 23 | return cnt |
| 24 | |
| 25 | |
| 26 | if __name__ == "__main__": |
no test coverage detected