Python Forum

Full Version: Removal of items in .txt using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I hv been self-learning Python on youtube videos. Today I visited the topic of file operation in which I would use Python to operate on notepad(txt files).
I managed to add stuff to a list of items using the write and append function. But I failed to make removal. For example,
Cat
Dog
Lion
Bear
These items are written in notepad. I want to delete one or more items from the list by user input. How can I do that? Thanks a lot in advance!
(Aug-30-2022, 05:46 PM)nanakochan Wrote: [ -> ]I managed to add stuff to a list of items using the write and append function. But I failed to make removal. For example,
Cat
Dog
Lion
Bear
These items are written in notepad. I want to delete one or more items from the list by user input. How can I do that?

It's not going to be so different from the code you have; maybe a simple access mode change, but as we can't see what code you have, unless you post it, we'll be guessing, which may or may not help you.
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.
When you say...

(Aug-30-2022, 05:46 PM)nanakochan Wrote: [ -> ]I managed to add stuff to a list of items using the write and append function.

... are you referring to the access modes w and a? Understand that if you open a file with access mode w, when you use the .write() method, you will overwrite the old file (and all that it contains) with a new version; whatever that is after your app have changed it, whereas access mode a will 'append' the changes to the old file, not replacing anything.

Does that make sense, or help?
Thank you for all your advice and suggestions.
I tried again. It still doesnt work. I failed to remove the item by user input. This is my code-

animalslist = open ('C:\\Users\\defaultuser100000\\animals.txt')
items = animalslist.read()
nlist = [items]
out = str(input("Enter items to be removed"))
if out == items:
    nlist.remove(out)
animalslist = open('C:\\Users\\defaultuser100000\\animals.txt', 'w' )
animalslist.write(items)
animalslist.close()
nlist = [items.replace('\n',' ') for items in nlist]
print ("You now have")
print (nlist)
print("in your list")
You are creating a list with one sting in line 3.
['Cat\nDog\nLion\nBear']
Also if use with open then do no need to close() the file object.
Example can do it like this.
with open ('animals.txt') as f:
    items = [i.strip() for i in  f]

user_choice = input("Enter items to be removed: ")
items.remove(user_choice)

with open('animals.txt', 'w') as f_out:
    f_out.write('\n'.join(items))
The most common way is that just write to a new file.
Can use fileinput in standard library to do in place.
There is a small library in-place that make this prosses of modify the original file in place easier.
I have used some time before.
Example,it also nice that keep a backup of original file if needed.
import in_place

user_choice = input("Enter items to be removed: ")
with in_place.InPlace('animals.txt', backup='animals_bak.txt') as fp:
    items = [ani.strip() for ani in fp if user_choice not in ani]
    fp.write('\n'.join(items))
You need to research what file.read() does. You need to research what a lot of Python commands do. An easy way to do this is run interactive Python.
I made a file that looks like this:
Output:
one two three
In a terminal I run python without specifying a .py file to start interactive python. Then I typed some commands and looked at what happened.
Output:
>>> file = open("test.txt", "r") >>> lines = file.read() >>> print(type(lines)) <class 'str'> >>> print(lines) one two three >>> print(repr(lines)) 'one\ntwo\nthree\n'
file.read() returned the contents of the file as a single string.
I could try replacing part of the string.
Output:
>>> new_lines = lines.replace("two", "") >>> print(new_lines) one three >>> print(repr(new_lines)) 'one\n\nthree\n'
That didn't work the way I wanted because I left in the newline after the word.
Output:
>>> new_lines = lines.replace("two\n", "") >>> print(new_lines) one three >>> print(repr(new_lines)) 'one\nthree\n'
That worked better. I don't like it, but it did work.

Next I am going to try file.readlines().
Output:
>>> file.close() >>> file = open("test.txt", "r") >>> lines = file.readlines() >>> file.close() >>> print(lines) ['one\n', 'two\n', 'three\n']
Now I get a list. I can remove an item from a list.
Output:
>>> new_lines = lines.remove("two") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list
The problem is I entered "two", but the item in the list is "two\n".
Output:
>>> new_lines = lines.remove("two\n") >>> print(new_lines) None >>> print(lines) ['one\n', 'three\n']
What? Why None? I lookup the list.remove() function and see that the function removes the item from the list, modifying the list. In Python this is often referred to as "in place", meaning that a mutable object is modified by the function instead of creating a new object with different values. in place functions almost always return None.

Notice that "two\n" is missing from list when I print list.

But you probably don't want to type in "two\n" when you want to remove "two". To fix that problem, you need to remove the newline character from each of the strings.
Output:
>>> file = open("test.txt", "r") >>> lines = file.readlines() >>> file.close() >>> print(lines) ['one\n', 'two\n', 'three\n'] >>> lines = [line.strip() for line in lines] >>> print(lines) ['one', 'two', 'three'] >>> lines.remove("two") >>> lines ['one', 'three']
I keep forgetting that interactive python will print values without having to use a print command.

To remove the newline characters from each of the "lines", I used a list comprehension. Think of it as a shorthand way of writing a for loop. These are roughly equivalent.
for index, line in enumerate(lines):
    lines[index] = line.strip()

lines = [line.strip() for line in lines]
You might also want to remove words that use different case.
Output:
>>> file = open("test.txt", "r") >>> lines = [line.strip() for line in file.readlines()] >>> lines ['One', 'Two', 'Three'] >>> lines = [line for line in lines if line.lower() != "two"] >>> lines ['One', 'Three']
Here I use a list comprehension again. This time with a condition. I only include lines that don't match "two". To make the condition case insensitive I convert the line to lower case before doing the comparison.
Not an answer to your question, but just put: Python tutorials in a search engine.

You will find many many sites that can help you. There are also tutorials here on this website!

I like realpython.com

https://www.w3schools.com/python lets you try things out directly

https://www.python.org/about/gettingstarted for absolute beginners

Then, if you get stuck, you can ask the experts here. (I'm not one of them!)

Always remember, there is usually more than one way to do what you want to do.

Have fun!
I think that if one is familiar with sed it can be accomplished using subprocess quite easily (removing row 'Cat' from file animals.txt):

>>> import subprocess
>>> subprocess.run('cat animals.txt', shell=True)                 # file content before
Dog
Lion
Cat
Bear
CompletedProcess(args='cat animals.txt', returncode=0)
>>> subprocess.Popen(['sed', '-i', '', '/Cat/d', 'animals.txt'])   # inplace deletion with sed
<Popen: returncode: None args: ['sed', '-i', '', '/Cat/d', 'animals.txt']>
>>> subprocess.run('cat animals.txt', shell=True)                # file content after
Dog
Lion
Bear
CompletedProcess(args='cat animals.txt', returncode=0)
Edit: I noticed, that using cat (concatenate and print files) and Cat can be confusing. Sorry about that.