PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » File Handling » Read File in Python

Read File in Python

Updated on: July 3, 2021 | Leave a Comment

In this article, we’ll learn how to read files in Python.

In Python, temporary data that is locally used in a module will be stored in a variable. In large volumes of data, a file is used such as text and CSV files and there are methods in Python to read or write data in those files.

After reading this tutorial, you’ll learn: –

  • Reading both text and binary files
  • The different modes for reading the file
  • All methods for reading a text file such as read(), readline(), and readlines()
  • Read text file line by line
  • Read and write files at the same time.

Table of contents

  • Access Modes for Reading a file
  • Steps for Reading a File in Python
    • Example: Read a Text File
  • Reading a File Using the with Statement
  • File Read Methods
  • readline(): Read a File Line by Line
    • Reading First N lines From a File Using readline()
    • Reading Entire File Using readline()
    • Reading First and the Last line using readline()
  • readlines(): Reading File into List
    • Reading first N lines from a file
    • Reading the last N lines in a file
  • Reading N Bytes From The File
  • Reading and Writing to the same file
    • Reading File in Reverse Order
  • Reading a Binary file

Access Modes for Reading a file

To read the contents of a file, we have to open a file in reading mode. Open a file using the built-in function called open(). In addition to the file name, we need to pass the file mode specifying the purpose of opening the file.

The following are the different modes for reading the file. We will see each one by one.

File ModeDefinition
rThe default mode for opening a file to read the contents of a text file.
r+Open a file for both reading and writing. The file pointer will be placed at the beginning of the file.
rbOpens the file for reading a file in binary format. The file pointer will be placed at the beginning of the file.
w+Opens a file for both writing as well as reading. The file pointer will be placed in the beginning of the file. For an existing file, the content will be overwritten.
a+Open the file for both the reading and appending. The pointer will be placed at the end of the file and new content will be written after the existing content.
file read modes

Steps for Reading a File in Python

To read a file, Please follow these steps:

  1. Find the path of a file

    We can read a file using both relative path and absolute path. The path is the location of the file on the disk.
    An absolute path contains the complete directory list required to locate the file.
    A relative path contains the current directory and then the file name.

  2. Open file in Read Mode

    To open a file Pass file path and access mode to the open() function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example, r is for reading.
    For example, fp= open(r'File_Path', 'r')

  3. Read content from a file.

    Once opened, we can read all the text or content of the file using the read() method. You can also use the readline() to read file line by line or the readlines() to read all lines.
    For example, content = fp.read()

  4. Close file after completing the read operation

    We need to make sure that the file will be closed properly after completing the file operation. Use fp.close() to close a file.

Example: Read a Text File

The following code shows how to read a text file in Python.

A text file (flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text.

In this example, we are reading all content of a file using the absolute Path.

Consider a file “read_demo.txt.” See the attached file used in the example and an image to show the file’s content for reference.