Python read() method – Python File read() Method with Examples

Files In Python:

Python read() method: A file is a piece of data or information stored on a computer’s hard drive. You’re already familiar with a variety of file kinds, including music, video, and text files. Manipulation of these files is trivial with Python. Text files and binary files are the two types of files that are commonly used. Binary files contain binary data that can only be read by a computer, whereas text files include plain text.

For programmers and automation testers, Python file handling (also known as File I/O) is a crucial topic. Working with files is required in order to write to or read data from them.

In addition, if you didn’t know, I/O activities are the most expensive techniques via which software might fail. As a result, when implementing file processing for reporting or any other reason, you should proceed with caution. The construction of a high-performance application or a robust solution for automated software testing can benefit from optimizing a single file activity.

File read() Method in Python:

Python read method: The read() method is a built-in method in Python that is used to read the content of a file.

The read() method reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file.

Syntax:

file.read()

Parameters

size: This is optional. The number of bytes that will be returned. The default value is -1, which means the entire file.

Return Value:

Read method python: This method’s return type is <class’str’>; it returns the string, i.e. the file’s content (if the file is in text mode).

File read() Method with Examples in Python

Example1

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Read the contents of the file using the read() function and print it.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read mode. In this case, we're simply reading the contents of the file.
read_file = open(givenFilename, 'r') 
# Read the contents of the file using the read() function and print it
print(read_file.read())

Output:

hello this is btechgeeks 
good morning btechgeeks
welcome all

Example2

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read mode. In this case, we’re simply reading the contents of the file.
  • Pass some random number to the read() function to read the specified number of bytes of the file and print it.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read mode. In this case, we're simply reading the contents of the file.
read_file = open(givenFilename, 'r') 
# Pass some random number to the read() function to read the specified number
# of bytes of the file and print it.
print(read_file.read(20))

Output Text:

hello this is btechg

File Content:

hello this is btechgeeks 
good morning btechgeeks
welcome all

Google Colab Images:

Files and Code: