Python program to delete a specific line from a file

This article was created to cover some programs in Python that delete any particular (specific) line from a given file. Here, both lines' content and the file's name must be entered by the user.

Things to Do Before the Program

Because the program given below is used to delete a specific line entered by the user from a given file, Therefore, we must create a file with some content inside it.

For example, create a file called codescracker.txt and put the following content in it:

Hello World
This is Python
I'm a File
My name is codescracker.txt

Save this file in the current directory. The directory where the Python program given below (to delete a specific line from a file) is saved Here is a snapshot of the opened file, codescracker.txt:

python delete specific line from file

Now let's move on to create a Python program that deletes any specific line from this file.

Delete a Specific Line from the File

This is a very basic version of the Python program to delete any required line from a file. Later on, we've modified the program:

print("Enter the Name of File: ")
fileName = input()
print("Enter the Line to Delete: ")
lineText = input()
fileHandle = open(fileName, "r")
lines = fileHandle.readlines()
fileHandle.close()
fileHandle = open(fileName, "w")
for line in lines:
  if line.strip("\n") != lineText:
    fileHandle.write(line)
fileHandle.close()
print("\nThe Line Deleted Successfully!")

Here is its sample run:

delete specific line from file python

Now enter the name of the newly created file, say "codescracker.txt," and then enter the line "I'm a File" to delete this line from the given file:

delete required line from file python

Now if you see the same file (as created earlier), the line gets deleted. Here is the new snapshot of the opened file, codescracker.txt:

remove specific line from file python

Modified Version of the Previous Program

This program is a modified version of the previous program. In this program, I've used the "try-except" block to handle exceptions (if any) thrown by the open() method while opening the file. The end is used to skip inserting an automatic newline.

print(end="Enter the Name of File: ")
fileName = input()
try:
  fileHandle = open(fileName, "r")
  lines = fileHandle.readlines()
  fileHandle.close()
  try:
    fileHandle = open(fileName, "w")
    print(end="Enter the Line to Delete: ")
    lineText = input()
    chk=0
    for line in lines:
      if line.strip("\n") != lineText:
        fileHandle.write(line)
      else:
        chk=1
    fileHandle.close()
    if chk==1:
      print("\nThe Line Deleted Successfully!")
    else:
      print("\nThe line doesn't exist!")
  except IOError:
    print("\nError Occurred while Opening the File (Writing Mode)!")
except IOError:
  print("\nThe File doesn't exist!")

Here is its sample run with user input, codescracker.txt as the file's name, and "Hello World" as the line to delete:

python remove specific lines from file

Here is another sample run:

remove particular line from file python

And here is the last sample run with user input, temp.txt, a non-existing file's name:

delete particular line from file python

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!