Python Forum
Removal of items in .txt using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removal of items in .txt using python
#3
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.
Reply


Messages In This Thread
RE: Removal of items in .txt using python - by deanhystad - Aug-30-2022, 06:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get all items in SharePoint recycle bin by using sharepy library in Python? QuangHuynh 2 505 Apr-10-2024, 03:09 PM
Last Post: SandraYokum
  Removal of duplicates teebee891 1 1,879 Feb-01-2021, 12:06 PM
Last Post: jefsummers
  concatenating 2 items at a time in a python list K11 3 2,443 Oct-21-2020, 09:34 AM
Last Post: buran
  how to use items combobox in table name sqlite in python hampython 1 2,763 May-24-2020, 02:17 AM
Last Post: Larz60+
  Access list items in Python kamaleon 2 2,448 Dec-31-2019, 11:10 AM
Last Post: kamaleon
  Vertical Seam Removal scott14 0 2,008 Dec-27-2018, 03:03 AM
Last Post: scott14
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,771 Mar-03-2018, 05:23 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020