Python Program to Delete Specific Line from File

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

Things to do Before Program

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

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

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

Save this file in current directory. The directory where the Python program given below (to delete specific line from a file) is saved. Here is the snapshot of opened file, fresherearth.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 through the program.

Delete a Specific Line from File

This is 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 newly created file say fresherearth.txt and then enter the line say 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 opened file, fresherearth.txt:

remove specific line from file python

Modified Version of Previous Program

This program is the modified version of previous program. In this program, I've used try-except block to handle exception (if any) thrown by 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, fresherearth.txt as file's name and Hello World as 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 »