Deleting certain lines from a text file is a common task that can be accomplished using Python. In this tutorial, we will show you how to do this step by step, using different methods and techniques to manipulate text files.
Method 1: Using a Temporary File
This method involves creating a temporary file and writing lines from the original file to the temporary file, excluding the lines you want to delete. Then, we’ll rename the temporary file to replace the original file.
Step 1: Create a Sample Text File
Let’s create a sample text file named sample_text.txt
with the following content:
This is line 1. This is line 2. This is line 3. This is line 4. This is line 5.
Step 2: Write Python Code to Delete Specific Lines
In this example, we will delete line 3 from the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Define the line number to delete. line_to_delete = 3 # Read the original file. with open("sample_text.txt", "r") as file: lines = file.readlines() # Write to a temporary file, skipping the specified line. with open("sample_text_temp.txt", "w") as temp_file: for i, line in enumerate(lines): if i + 1 != line_to_delete: temp_file.write(line) # Replace the original file with the temporary file. import os os.remove("sample_text.txt") os.rename("sample_text_temp.txt", "sample_text.txt") |
Output:
After executing the code, the content of the sample_text.txt
will be:
This is line 1. This is line 2. This is line 4. This is line 5.
Method 2: Using a List Comprehension
This method involves reading the entire content of the file into memory, modifying it, and then writing the changes back to the file.
Step 1: Write Python Code to Delete Specific Lines
In this example, we will delete lines 2 and 4 from the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Define the line numbers to delete. lines_to_delete = [2, 4] # Read the original file. with open("sample_text.txt", "r") as file: lines = file.readlines() # Update the content of the file using list comprehension. lines = [line for i, line in enumerate(lines) if i + 1 not in lines_to_delete] # Write the new content to the file. with open("sample_text.txt", "w") as file: file.writelines(lines) |
Output:
After executing the code, the content of the sample_text.txt
will be:
This is line 1. This is line 3. This is line 5.
Full Code
Method 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import os line_to_delete = 3 with open("sample_text.txt", "r") as file: lines = file.readlines() with open("sample_text_temp.txt", "w") as temp_file: for i, line in enumerate(lines): if i + 1 != line_to_delete: temp_file.write(line) os.remove("sample_text.txt") os.rename("sample_text_temp.txt", "sample_text.txt") |
Method 2:
1 2 3 4 5 6 7 8 9 |
lines_to_delete = [2, 4] with open("sample_text.txt", "r") as file: lines = file.readlines() lines = [line for i, line in enumerate(lines) if i + 1 not in lines_to_delete] with open("sample_text.txt", "w") as file: file.writelines(lines) |
Conclusion
In this tutorial, we have demonstrated two different methods of deleting specific lines from a text file using Python. The first method created a temporary file, while the second method utilized list comprehension, both methods resulting in a modified text file with the desired lines removed.