How to Delete Specific Line from a Text File in Python?

In the following tutorial, we will learn the method of deleting a specific line from a text file in the Python programming language.

So, let's get started.

Removing Specific Line from Text File

Let's say we have a text file called TextFile.txt that contains some random content. We are going to remove a certain line (let's say line 2) from a text DOCUMENT.

TextFile.txt

Algorithm:

The stages and algorithm to achieve the desired goal are listed below −

  • To save the text file's path, create a variable.
  • To open a text file in read-only mode, use the open() method, which takes two arguments: the file name and mode (where "r" stands for read-only mode). This function opens a file and returns a file object as a result.

with open(inputFile, 'r') as file data:

  • use the readlines() method, which generates a list with each line in the file represented by an item in the list. Use the hint option to restrict the amount of lines that are returned. To retrieve the list of lines from a particular input text file, specify the maximum number of bytes to be returned (if more than that, no further lines are returned).

file.readlines(hint)

  • Make a variable and set its initial value to 1 to store the line number.
  • Open the specified text file in write mode by supplying the file name and mode as arguments to the open() method (which opens a file and returns a file object as a result); write mode is indicated by the letter "w" in this example.

with open(inputFile, 'w') as file data:

  • Use the for loop to iterate through every line in the file.
  • Using the input() method, enter the line number that has to be removed as dynamic input. This function reads a line from the user's input, removes the trailing newline to convert it to a string, and then returns the result. An EOFError exception is raised when EOF is encountered.) Then, use the int() method to typecast it to an integer (converts to an integer).
  • Find out if the line index, or line number, is equal to a specified delete line number by using the if conditional expression.
  • If the condition is true, use the write() method, which writes a given text to a file. The supplied text will be placed based on the file mode and stream location to write the matching line into a file.
  • Add one to the line index (or line number) value.
  • Upon successful deletion of the specified line, print a random string.
  • After removing the specified line, use the open() method to open the input file again in read mode and output the contents.
  • Use the for loop to iterate through every line in the file.
  • Print the text file, line by line.
  • Use the close() method to end the input file (used to shut an opened file).

Example:

The program that follows may be used to remove a specific line from a text file and then print the contents of the result file.

Output:

When the program above runs, it will provide the following results:

 
Line 2  is deleted successfully

File Content After Deletion :
Hello Everyone.
This is a sample file.
We are happy to learn
Python from Javatpoint.
The Resources from this
website are incredible.
Everything we have learned so
far helped us in our career.
We should learn new technologies
everyday and enhance our knowledge
and develop our skills
to be in the competition.   

We opened a text file with some random content in our program's reading mode after providing it with it. The starting line number, 1, was the first value of the variable we established to hold the current line number. After reading through the whole file, we made sure the number entered by the user matched the line that needed to be removed. If the answer is false, we write it to the file instead of deleting or removing it. The eliminated line does not show in the output file because we added the remaining lines to the file rather than removing the specified line. The value of the line number increases by one for each line.

Conclusion

This article explains how to remove a particular line from a text file while keeping the rest of the content intact. We also learned how to read and write data into text files and navigate through a text file from beginning to end.