MCPcopy Index your code
hub / github.com/geekcomputers/Python / text_file_replace

Function text_file_replace

text_file_replace.py:13–23  ·  view source on GitHub ↗
(file, encoding, old, new)

Source from the content-addressed store, hash-verified

11
12
13def 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
26if __name__ == "__main__":

Callers 1

Calls 2

countMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected