In this tutorial, we will learn how to delete a line in a text file using Python. The process can be quite useful in various instances, where you may need to manipulate text files by removing specific lines.
We will cover two popular methods of achieving this goal. The first method is to read the contents of a file, remove a line, and then overwrite the original file.
The second method is to open both the input and output files, read a line from the input file, and then write it to the output file except the line you want to delete.
Step 1: Create a sample text file
First, let’s create a sample text file with some lines. Here’s an example (example.txt):
1 2 3 4 5 |
Line 1: Hello, World! Line 2: Python is fun! Line 3: Let's delete some lines. Line 4: Another line. Line 5: End of the file. |
Step 2: Deleting a line by overwriting the original file
To delete a line using this method, follow these steps:
1. Read the contents of the text file and store it in a list, where each line is an element in the list.
2. Remove the line you want to delete from the list.
3. Overwrite the original file with the modified list.
Here’s the code to remove line 3 from example.txt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
file_name = "example.txt" deleted_line_no = 2 # Note: list index starts from 0, so 3rd line is at index 2 # Read file contents with open(file_name, "r") as file: lines = file.readlines() # Remove the specified line from the list del lines[deleted_line_no] # Write the modified lines back to the file with open(file_name, "w") as file: file.writelines(lines) print("Line deleted successfully.") |
Step 3: Deleting a line using input and output files
To delete a line using this method, follow these steps:
1. Open both the input file and a temporary output file.
2. Read a line from the input file.
3. Write it to the output file if the line does not match the line you want to delete.
4. Repeat steps 2 and 3 for all lines in the input file.
5. Close both files.
6. Replace the original file with the temporary output file.
Here’s the code to remove line 3 from example.txt using input and output files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
input_file = "example.txt" output_file = "example_temp.txt" deleted_line_no = 2 # Open both input and output files with open(input_file, "r") as infile, open(output_file, "w") as outfile: for i, line in enumerate(infile): # If the line does not match the line to be deleted, write it to the output file if i != deleted_line_no: outfile.write(line) # Replace the original file with the output file import os os.replace(output_file, input_file) print("Line deleted successfully.") |
Full code samples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Method 1: Overwriting the original file file_name = "example.txt" deleted_line_no = 2 with open(file_name, "r") as file: lines = file.readlines() del lines[deleted_line_no] with open(file_name, "w") as file: file.writelines(lines) print("Line deleted successfully.") # Method 2: Using input and output files input_file = "example.txt" output_file = "example_temp.txt" deleted_line_no = 2 with open(input_file, "r") as infile, open(output_file, "w") as outfile: for i, line in enumerate(infile): if i != deleted_line_no: outfile.write(line) import os os.replace(output_file, input_file) print("Line deleted successfully.") |
Conclusion
In this tutorial, we’ve covered two different methods to delete a line in a text file using Python. The first method is by reading the contents of the file, removing the specific sequence, and then overwriting the original file.
The second method is using both input and output files, reading lines from the input file, and writing them to the output file except for the line we want to delete. Both methods can be used depending on your preference and use case.