Because Python provides no direct method for deleting a specific line in a file, it’s necessary to find our own approach.
In this guide, we’ll cover several ways of removing lines from a text file using Python. We’ll see how to remove lines based on their position in the document and how to delete content that matches a string.
We’ll also cover examples of using custom logic to tackle more challenging problems. It doesn’t matter if we’re working with a simple text file, or more complicated comma-separated files (CSV), these techniques will help you manage your data.
We can use Python to handle both large and small files in a memory efficient manner.
Using a Number to Delete a Line
In our first example, we’ll look at removing a line based on it’s position in the file. Starting with a randomly generated list of names saved on our computer, we’ll use Python to delete a name from the list based on the order it appears in the list.
The file is called names.txt and it’s saved in the same directory as our python file. Our goal is to delete the 7th line in the file.
In Python, we can use the with statement to safely open files. With the file open, we’ll employ the readlines() method to retrieve a list containing the file’s contents.
That’s all for reading the list of names. Next, we’ll use another with statement to open the file again, this time in write mode.
Using a for loop to iterate over the lines of the file, we also make use of a variable to keep track of the current line number. When we reach the line we want to remove, an if statement makes sure we skip the line.
Let’s go over the steps one more time:
- Open the file in read mode
- Read the files contents
- Open the file in write mode
- Use a for loop to read each line and write it to the file
- When we reach the line we want to delete, skip it
Because we’re using a Python with statement to handle the file, there’s no need to close it after we’re done. Python takes care of that for us.
names.txt
1 Amina,Waelchi
2 Sharon Reynolds
3 Lilian Hane
4 Felicita Howell
5 Sallie Senger
6 Lucile Schuster
7 Emmitt Schuppe
8 Rowena Leffler
9 Hipolito Batz
10 Gia Hill
Example 1: Remove a line based on a specified line number
def remove_line(fileName,lineToSkip):
""" Removes a given line from a file """
with open(fileName,'r') as read_file:
lines = read_file.readlines()
currentLine = 1
with open(fileName,'w') as write_file:
for line in lines:
if currentLine == lineToSkip:
pass
else:
write_file.write(line)
currentLine += 1
# call the function, passing the file and line to skip
remove_line("names.txt",7)
By wrapping our logic in a function, we can easily remove a line from a file by calling remove_lines() and passing the name of the file and the number of the line we’d like to remove.
If we plan to use a block of Python code more than once, it’s a good idea to wrap it in a function. Doing so will save us time and energy.
Delete a Line by Matching Content
We’ve seen how to remove content from a file based on its line position. Now we’ll look at how to delete a line that matches a given string.
We have a catalogue of nursery rhymes, but someone has played a bit of mischief on us. In an ironic stroke, they’ve added the line “This line doesn’t belong” to our files!
There’s no need to panic. We can use Python to easily undo the mischief.
In our Python code, we’ll start by reading the file, named itsy_bitsy.txt, and storing its content in a variable called lines.
Just like in the previous example, we’ll use Python with statements to open the file. In order to find the matching line, we’ll need to remove the newline characters that readlines() tacks on to the end of every string.
We can remove the newline character using the strip() function. This is a built-in function that removes characters from the beginning or end of a string.
When the matched content is found, we’ll use an if statement to pass it over, effectively removing it from the old file.
itsy_bitsy.txt
The itsy bitsy spider climbed up the waterspout.
Down came the rain
And washed the spider out.
Out came the sun
This line doesn’t belong
And dried up all the rain
And the itsy bitsy spider climbed up the spout again.
Example 2: Matching content and removing it from a file
with open("itsy_bitsy.txt", 'r') as file:
lines = file.readlines()
# delete matching content
content = "This line doesn't belong"
with open("itsy_bitsy.txt", 'w') as file:
for line in lines:
# readlines() includes a newline character
if line.strip("\n") != content:
file.write(line)
Using Custom Logic to Delete a Line in Python
When dealing with file data, we’ll often need custom tailored solutions to meet our needs. In the following examples, we’ll explore using custom logic to solve a variety of data problems.
By tailoring our solutions, it’s possible to solve more difficult problems. For example, what if we’d like to delete a line from a file, but only know part of it?
Even if we only know a single word, we can use Python to find the line we need to remove. By taking advantage of Python’s built-in methods, we’ll see how to solve custom challenges with Python code.
Remove a line with a specific string
In the next exercise, we’ll see how to remove a line that contains a part of a string. Building on the knowledge gained from the previous examples, it’s possible to delete a line containing a given substring.
In Python, the find() method can be used to search a string for a substring. If the string contains the substring, the function returns an index representing its position. Otherwise, the method returns -1.
In a text file named statements.txt, we have a list of randomly generated sentences. We need to remove any sentence that contains the given substring.
By using find(), we’ll know if a line contains the string we’re looking for. If it does, we’ll remove it from the file.
Here’s the syntax for using find():
mystring.find(substring)
statements.txt
He didn’t heed the warning about the banana.
My friend took the apples to the market.
She bought a farm that grows peaches.
There is a lovely grape orchard beyond the hills.
She’s absolutely nuts about her new car.
Example 3: Delete a line that contains a given string
# remove a line containing a string
with open("statements.txt",'r') as file:
lines = file.readlines()
with open("statements.txt",'w') as file:
for line in lines:
# find() returns -1 if no match is found
if line.find("nuts") != -1:
pass
else:
file.write(line)
Remove the shortest line in file
Let’s take another look at statement.txt. Some changes have been made.
statements.txt
He didn’t heed the warning about the Banana.
My friend took the apples to the market.
She bought a farm that grows peaches.
He claims to have seen a UFO.
There is a lovely grape orchard beyond the hills.
There was little to eat on the island besides coconuts.
We’ve added some new lines. This time, we need to remove the shortest line in the document. We can do this by using the len() method to find the length of each line.
By comparing the lengths of the lines, it’s possible to find the shortest line. Afterwards, we can use a with statement open and remove the line from the file.
Example 4: Delete the shortest line in a file using the len() method
# remove the shortest line from statements.txt
with open("statements.txt",'r') as read_file:
lines = read_file.readlines()
shortest = 1000 # used to compare line length
lineToDelete = "" # the line we want to remove
for line in lines:
if len(line) < shortest:
shortest = len(line)
lineToDelete = line
with open("statements.txt",'w') as write_file:
for line in lines:
if line == lineToDelete:
pass
else:
write_file.write(line)
Summary
With this post we’ve covered several methods of deleting lines from files in Python. We saw that we can delete lines based on their position in a file using a for loop.
We can also remove files that match content by comparing strings, either with the == operator, or by using the find() method.
These are only some of the ways one can remove lines from a file in Python.
Related Posts
If you’d like to learn more about working with strings and file data in Python, follow the links below.
- Joining string with Python string concatenation
- How to use a Python dictionary for better data management
- Using Python list comprehension to streamline your code
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.