• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Difference Between read(), readline() and readlines() in Python

Difference Between read(), readline() and readlines() in Python

July 25, 2022 Leave a Comment

When reading files in Python, there are a few different functions you can use to extract text from a file.

The three main functions you can use to read content from a file are read(), readline() and readlines().

read() reads the entire file and returns a string, readline() reads just one line from a file, and readlines() returns a list of strings representing the lines of the file.

In the rest of this article, we will go into the details of each function and the differences between read(), readline() and readlines()


The power of programming in Python is that there are many ways you can accomplish similar actions. With this flexibility, it can be tricky to understand the differences between certain functions.

One such situation is when you are performing file input and output and want to read or write to files.

When reading files in Python, there are a few different functions you can use to extract text from a file: read(), readline() and readlines().

Let’s talk about how you can use each of these functions in Python to read text from a file.

How to Use read() to Read Entire File in Python

The Python file read() function allows us to read an entire file at once and store it in a string. Depending on the size of your file, this could make sense for you and your application.

Below is a simple example showing you how to use read() in Python.

with open("example.txt") as f:
    content = f.read()

One example of a case where you would use read() is if you want to check if a string is in a file.

In this case, you read in the file and then check if a given string is in the returned text.

string = "word"
in_file = False

with open("example.txt","r") as f:
    if string in f.read():
        in_file = True

print(in_file)

#Output:
True

Typically though, it usually is easier to use readline() or readlines() and work with the lines, instead of the entire file.

How to Use readlines() to Read All Lines of File in Python

The next function you can use to read content from a file is the readlines() function. readlines() reads all of the lines and returns a list of strings.

Using readlines() can be useful if you are going to process the file line by line, or want to extract certain lines out of a file.

Below is a simple example showing you how to use readlines() in Python.

with open("example.txt") as f:
    lines = f.readlines()

One example of where you might use readlines() is if you want to read the last N lines of a file.

To read the last N Lines of a file in Python, the easiest way is to use the readlines() function and then access the last N elements of the returned list.

n = 5

with open("example.txt") as f:
    last_n_lines = f.readlines()[-n:]

One other example is if you want to remove whitespace from the lines of a file.

When reading the contents of a file, whitespace sometimes can cause us troubles. To remove whitespace from each line when using Python, you can use the Python strip() function with readlines()

myfile = open("example.txt", "r")

lines = myfile.readlines()

for line in lines:
    stripped_line = line.strip()

Using readline() to Read Lines of File in Python

The last function you can use to read content from a file is the readline() function. When you open a file in Python, Python returns a generator and you can iterate over the lines with this generator.

For example, when you open a file, we are “pointing” at the first line and so when you use readline(), you can read the first line of the file.

Below shows you a simple example of how to use readline() in Python.

with open("example.txt") as f:
    first_line = f.readline()

If you want to read multiple lines, then you can use readline() multiple times.

with open("example.txt") as f:
    first_line = f.readline()
    second_line = f.readline()
    third_line = f.readline()

readline() can be useful if you are doing processing and just want to access particular line, but in the examples we have gone over here, readlines() and read() typically give you more flexibility to work with files.

Hopefully this article has been useful for you to learn about the differences between read(), readline() and readlines() in Python.

Other Articles You'll Also Like:

  • 1.  Get List of Letters in Alphabet with Python
  • 2.  Convert pandas Series to Dictionary in Python
  • 3.  Using Python to Create Empty DataFrame with pandas
  • 4.  Convert List to Set with Python
  • 5.  Using Python to Reverse Tuple
  • 6.  Python Destroy Object – How to Delete Objects with del Keyword
  • 7.  Get First Digit in Number Using Python
  • 8.  Get Days in Month from Date in pandas DataFrame
  • 9.  Check if Line is Empty in Python
  • 10.  Check if pandas DataFrame is Empty in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x