File I/O in Python - Read operations

The file read in python includes text, binary and raw types. Read operations with file object on all these types are explained here with examples.

File Input Updated 23 Sep 2016 3 min read

The file read operations in Python are of three types.

  1. Read file contents as text (Text Mode)
  2. Read file contents as binary (Binary Mode)
  3. Read file contents as binary buffering (Raw mode)

1. Reading file contents as text in Python

The built-in function open() by default opens a file in text mode. The contents read on the file object are returned as Python string - a str object.

(a) Example: Read by specifying the byte count:

python
# Example Python program that reads a file in text mode.

            import os

filePath    = "./alice.txt"

            # Open the file for reading. Default mode is text

            file         = open(filePath, "r")

# Number of bytes to read

            numBytes    = os.path.getsize(filePath)

            print("Number of bytes to read:")

            print(numBytes)

# Read the contents in one go

            fileContents    = file.read(numBytes)

            print("File contents:")

            print(fileContents)

            print(type(fileContents))

Output

Output
Number of bytes to read:

            97

            File contents:

            “Where should I go?" -Alice. "That depends on where you want to end up." - The Cheshire Cat.”

            <class 'str'>

(b) Example: Read the whole file:

python
file         = open(filePath, "r")

            contents     = file.read()

            print("File Contents:")

            print(contents)

The above example will read the whole file contents into the variable contents and print to the console. Care must be taken to make sure that there is enough physical memory to support this read operation, which reads any file contents in one go.

(c) Read the file line by line:

python
file = open(filePath, "r")

            aLine = ''

            while(True):

                aLine = file.readline()

                if aLine is not '':

                    print(aLine)

                else:

                    break

The above example will read line by line. EOF or End of File in Python is an empty character. When the EOF character is found, the program will break and exit.

However, while(True) is not recommended. Remember, writing

python
while (aLine = PythonFileObject.readline() is not '')

is not supported in Python.

So what is the better way to read multiple lines one at a time in Python? One can write using for loop like below.

python
for aLine in file:

                print(aLine)

2. Read file contents as binary:

While reading a file in binary mode, the programmer can specify the offset from the beginning of the file to read from.

python
# Example Python program that opens a file

            # in binary mode and reads the file contents

            # as bytes

            filePath = "dante.txt"

# Open the file to read in binary mode

            binFile = open(filePath, "rb")

# Move "n" positions

            nPos = 20

            binFile.seek(nPos)

# Read one line

            ln = binFile.readline()

            print(ln)

Since the file contents are read as binary, the output is printed like below.

Output
b'.. for it is the beginning of always\xe2\x80\x9d\n'

3. Read file contents as raw bytes:
In raw file I/O mode the file is read as binary without any buffering which means any optimisation like buffered reading to be done by the programmer herself. Raw I/O in Python is achieved by turning off the buffering with a zero value for the buffering parameter in the open() method.

python
# Example Python program that reads a file in raw mode

            # and prints the contents as raw bytes

filePath     = "dilbert.txt"

# Open in raw mode - no buffering

            fileRaw     = open(filePath, "rb", buffering=0)

# Move past "n" bytes

            fileRaw.seek(8)

# Read the remaining bytes and print them

            rawBytes = fileRaw.read()

            print(rawBytes)

Output:

Output
b"what you need, and I'll tell you how to get along without it."

The output is very much similar to the binary mode. However since buffering is disabled in Raw I/O a noticeable performance difference
could be found while reading a file with lot of data.