text_remove
text_remove
A module that provides functionality to remove a specified word from a text file.
Functions
| Name | Description |
|---|---|
| text_remove | Remove all occurrences of a whole word from a text file and write to a new file. |
text_remove
text_remove.text_remove(input_path, output_path, string_to_remove)Remove all occurrences of a whole word from a text file and write to a new file.
The function reads the input file using UTF-8 encoding, removes every occurrence of string_to_remove as a whole word (not as a substring), and writes the resulting text to the output file using UTF-8 encoding.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| input_path | str | Path to the input text file. Must be a string ending with .txt. |
required |
| output_path | str | Path to write the processed text file. Must be a string ending with .txt. If input_path and output_path are the same, the input file will be overwritten. |
required |
| string_to_remove | str | The word to remove from the input text. Must be a non-empty string. | required |
Returns
| Name | Type | Description |
|---|---|---|
| None | Writes output to file and returns None. |
Raises
| Name | Type | Description |
|---|---|---|
| TypeError | If input_path or output_path is not a string, or if string_to_remove is not a string. |
|
| ValueError | If a path does not end with .txt or if string_to_remove is empty. |
|
| FileNotFoundError | If the input file does not exist, or if the output directory does not exist. | |
| OSError | If an error occurs during file reading/writing. |
Examples
>>> text_remove("example.txt", "removed.txt", "the")