Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 21

What is the difference between open() in 'r' and 'rb' mode?

Answer:

The open() function in Python can open files in various modes, which determine how the file is to be handled. Two common modes for reading files are 'r' (read text mode) and 'rb' (read binary mode). Here's a detailed explanation of the differences between these modes:

'r' Mode (Read Text Mode)

  • Purpose: Opens the file for reading in text mode.
  • Usage: open('filename', 'r') or simply open('filename').
  • Data Handling:
    • Reads the file as a string.
    • Automatically decodes the file content using a specified or default encoding (usually UTF-8).
    • End-of-line characters (\n) are translated to the newline character \n in Python strings.
  • Applications: Suitable for reading text files like .txt, .csv, .json, etc.

Example:

with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

In this example:

  • The file example.txt is opened in read text mode.
  • The content of the file is read as a string and printed.

'rb' Mode (Read Binary Mode)

  • Purpose: Opens the file for reading in binary mode.
  • Usage: open('filename', 'rb').
  • Data Handling:
    • Reads the file as bytes.
    • No decoding is performed; the data is read as-is.
    • No translation of end-of-line characters; the bytes are read exactly as they are stored in the file.
  • Applications: Suitable for reading binary files like images, videos, executables, or any file that is not plain text.

Example:

with open('example.jpg', 'rb') as file:
    content = file.read()
    print(content[:20])  # Print the first 20 bytes

In this example:

  • The file example.jpg is opened in read binary mode.
  • The content of the file is read as bytes and the first 20 bytes are printed.

Key Differences

  1. Data Type:

    • 'r': Reads data as a string.
    • 'rb': Reads data as bytes.
  2. Encoding:

    • 'r': Automatically decodes the file content using a specified or default encoding (e.g., UTF-8).
    • 'rb': No decoding; data is read as raw bytes.
  3. End-of-Line Characters:

    • 'r': Translates end-of-line characters (\r\n or \r) to the newline character \n.
    • 'rb': No translation; reads end-of-line characters as they are.
  4. Usage:

    • 'r': Used for reading text files.
    • 'rb': Used for reading binary files.

Summary

  • 'r' Mode:

    • Opens a file for reading as text.
    • Data is returned as a string.
    • Automatically decodes based on file encoding.
    • End-of-line characters are translated to \n.
  • 'rb' Mode:

    • Opens a file for reading as binary.
    • Data is returned as bytes.
    • No decoding; reads raw data.
    • No translation of end-of-line characters.

Choosing between 'r' and 'rb' depends on the nature of the file you are reading. Use 'r' for text files and 'rb' for binary files to ensure the data is handled correctly.

Recent job openings