Aug-30-2022, 06:07 PM
(This post was last modified: Aug-30-2022, 06:07 PM by deanhystad.)
You cannot remove text from a file. This is a limitation of files, not Python. Instead you rewrite the file without the text you want to exclude. A text remove program usually has the following steps.
1. Open the file you want to modify in read mode.
2. Read all the lines from the file and save them as a list.
3. Close the file.
4. Open the file you want to modify in write mode. This deletes the contents of the file.
5. In a loop looking at each line your read in step 2:
a. Scan line for words you want to remove
b. Create a new line that doesn't contain the unwanted words.
c. Write the modified line to the file.
Sometimes this is done as a two file process. Reading a line from the original file and writing to a new file. When complete you can leave both files, or delete the old file and rename the new file using the old filename.
This is a pretty tricky programming problem, so don't expect to get it right the first try.
1. Open the file you want to modify in read mode.
2. Read all the lines from the file and save them as a list.
3. Close the file.
4. Open the file you want to modify in write mode. This deletes the contents of the file.
5. In a loop looking at each line your read in step 2:
a. Scan line for words you want to remove
b. Create a new line that doesn't contain the unwanted words.
c. Write the modified line to the file.
Sometimes this is done as a two file process. Reading a line from the original file and writing to a new file. When complete you can leave both files, or delete the old file and rename the new file using the old filename.
This is a pretty tricky programming problem, so don't expect to get it right the first try.