How to Delete a Line from a File in Python?

5/5 - (1 vote)

Problem Formulation and Solution Overview

๐Ÿ’ก This article will show you how to delete a line from a file in Python.

To make it more interesting, we have the following running scenario:

Rivers Clothing has a flat text file, rivers_emps.txt containing employee data. What happens if an employee leaves? They would like you to write code to resolve this issue.

Contents of rivers_emps.txt

100:Jane Smith
101:Daniel Williams
102:Steve Markham
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson

๐Ÿ’ฌ Question: How would we write code to remove this line?

We can accomplish this task by one of the following options:


Method 1: Use List Comprehension

This example uses List Comprehension to remove a specific line from a flat text file.

orig_lines = [line.strip() for line in open('rivers_emps.txt')]
new_lines = [l for l in orig_lines if not l.startswith('102')]

with open('rivers_01.txt', 'w') as fp:
    print(*new_lines, sep='\n', file=fp)

The above code uses List Comprehension to read in the contents of a flat text file to a List, orig_lines. If output to the terminal, the following displays.

['100:Jane Smith', '101:Daniel Williams', '102:Steve Markham', '103:Howie Manson', '104:Wendy Wilson', '105:Anne McEvans',
'106:Bev Doyle', '107:Hal Holden', '108:Mich Matthews',
'109:Paul Paulson']

Then, List Comprehension is used again to append each element to a new List only if the element does not start with 102. If output to the terminal, the following displays.

['100:Jane Smith', '101:Daniel Williams', '103:Howie Manson', '104:Wendy Wilson', '105:Anne McEvans', '106:Bev Doyle', '107:Hal Holden', '108:Mich Matthews', '109:Paul Paulson']

As you can see, the element starting with 102 has been removed.

Next, a new file, rivers_01.txt, is opened in write (w) mode and the List created above is written to the file with a newline (\n) character appended to each line. The contents of the file are shown below.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson
Python One-Liner Trick 9 - Nested List Comprehension

Method 2: Use List Comprehension and Slicing

This example uses List Comprehension and Slicing to remove a specific line from a flat text file.

orig_lines = [line.strip() for line in open('rivers_emps.txt')]
new_lines = orig_lines[0:2] + orig_lines[3:] 

with open('rivers_02.txt', 'w') as fp:
    fp.write('\n'.join(new_lines))

The above code uses List Comprehension to read in the contents of a flat text file to a List, orig_lines. If output to the terminal, the following displays.

['100:Jane Smith', '101:Daniel Williams', '102:Steve Markham', '103:Howie Manson', '104:Wendy Wilson', '105:Anne McEvans', '106:Bev Doyle', '107:Hal Holden', '108:Mich Matthews', '109:Paul Paulson']

Then Slicing is used to extract all elements, except element two (2). The results save to new_lines. If output to the terminal, the following displays.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson

As you can see, element two (2) has been removed.

Next, a new file, rivers_02.txt, is opened in write (w) mode and the List created above is written to the file with a newline (\n) character appended to each line. The contents of the file are shown below.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson
The Ultimate Guide to Slicing in Python

Method 3: Use Slicing and np.savetxt()

This example uses List Comprehension, Slicing and NumPy’s np.savetxt() function to remove a specific line from a flat text file.

Before moving forward, please ensure that the NumPy library is installed to ensure this code runs error-free.

import numpy as np 
orig_lines = [line.strip() for line in open('rivers_emps.txt')]
new_lines = orig_lines[0:2] + orig_lines[3:] 
np.savetxt('rivers_03.txt', new_lines, delimiter='\n', fmt='%s')

The first line imports the NumPy library.

The following line uses List Comprehension to read the contents of a flat text file to the List, orig_lines. If output to the terminal, the following displays.

['100:Jane Smith', '101:Daniel Williams', '102:Steve Markham', '103:Howie Manson', '104:Wendy Wilson', '105:Anne McEvans', '106:Bev Doyle', '107:Hal Holden', '108:Mich Matthews', '109:Paul Paulson']

Then Slicing is applied to extract all elements, except element two (2). The results save to new_lines. If output to the terminal, the following displays.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson

As you can see, element two (2) has been removed.

The last code line calls np.savetxt() and passes it three (3) arguments:

  • The filename (‘rivers_03.txt‘).
  • An iterable, in this case, a List (new_lines).
  • A delimiter (appended to each line) – a newline character (\n).
  • The format. Strings are defined as %s.

The contents of rivers_03.txt displays below.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson
NumPy Tutorial - Everything You Need to Know to Get Started

Method 4: Use pop()

This example uses the pop() function to remove a specific line from a flat text file.

import numpy as np 
orig_lines = [line.strip() for line in open('rivers_emps.txt')]
orig_lines.pop(2)
np.savetxt('rivers_04.txt', orig_lines, delimiter='\n', fmt='%s')

The first line imports the NumPy library.

The following line uses List Comprehension to read in the contents of a flat text file to the List, orig_lines. If output to the terminal, the following displays.

['100:Jane Smith', '101:Daniel Williams', '102:Steve Markham', '103:Howie Manson', '104:Wendy Wilson', '105:Anne McEvans', '106:Bev Doyle', '107:Hal Holden', '108:Mich Matthews', '109:Paul Paulson']

Then, the pop() method is called and passed one (1) argument, the element’s index to remove.

In this case, it is the second element.

If this List was output to the terminal, the following would display.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson

As shown in Method 3, the results save to a flat text file. In this case, rivers_04.txt. The contents are the same as in the previous examples.

๐Ÿ’กNote: The pop() function removes the appropriate index and returns the contents to capture if necessary.


Method 5: Use remove()

This example uses the remove() function to remove a specific line from a flat text file.

import numpy as np 
orig_lines = [line.strip() for line in open('rivers_emps.txt')]
orig_lines.remove('102:Steve Markham')
np.savetxt('rivers_05.txt', orig_lines, delimiter='\n', fmt='%s')

This code works exactly like the code in Method 4. However, instead of passing a location of the element to remove, this function requires the contents of the entire line you to remove.

Then, the remove() function is called and passed one (1) argument, the index to remove. In this case, it is the second element. If this List was output to the terminal, the following would display.

100:Jane Smith
101:Daniel Williams
103:Howie Manson
104:Wendy Wilson
105:Anne McEvans
106:Bev Doyle
107:Hal Holden
108:Mich Matthews
109:Paul Paulson

As shown in the previous examples, the results save to a flat text file. In this case, rivers_05.txt.

Python List remove()

Bonus: Remove row(s) from a DataFrame

CSV files are also known as flat-text files. This code shows you how to easily remove single or multiple rows from a CSV file

import pandas as pd
import numpy as np

staff = {
    'First' : ['Alice', 'Micah', 'James', 'Mark'],
    'Last'  : ['Smith', 'Jones', 'Watts', 'Hunter'],
    'Rate'  : [30, 40, 50, 37],
    'Age'   : [23, 29, 19, 45]}

indexes=['FName', 'LName', 'Rate', 'Age']
df = pd.DataFrame(staff, index=indexes)

df1 = df.drop(index=['Age'])
df.to_csv('staff.csv', index=False)

โœจFinxter Challenge
Find 2 Additional Ways to Remove Lines


Summary

This article has provided five (5) ways to delete a line from a file to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source xkcd