Replaces all instances of a string with another string in a text file, then writes the result into another file. If the old string does not exist in the file, then the output will be the same unaltered file.
Parameters
Name
Type
Description
Default
input_path
str
An existing file path to the text file to read. The file must be readable.
required
output_path
str
The file path to write the replaced text to. The file, should it exist, must be writeable.
required
old
str
A (non-empty) string to replace.
required
new
str
The replacement string.
required
Returns
Name
Type
Description
None
Raises
Name
Type
Description
TypeError
If input_path, output_path, old, or new is not a string.
ValueError
If old is an empty string, or the input_path or output_path are not paths to .txt files.
Examples
>>> text_replace("example.txt", "replaced.txt", "color", "colour")# replaced.txt contains the contents of example.txt, but with all# instances of 'color' replaced with "colour"