Python File read() Method



The Python File read() method reads the contents of a file. This method reads the whole file, by default; and only specified bytes, if an optional argument is accepted. Even if the file contains more characters than the mentioned size, the remaining characters in the file are neglected. If the read() method hits EOF before obtaining all the bytes, then it reads only available bytes in the file.

This method will only be executed if and only if a file is opened in any of the read modes. These read modes are either the only read mode (r), Read and Write mode (r+), and Append and Read mode(a+).

Syntax

Following is the syntax for the Python File read() method −

fileObject.read(size);

Parameters

  • size − (Optional Parameter) This is the number of bytes to be read from the file.

Return Value

This method returns the bytes read in string.

Example

Consider an existing file containing 5 lines as given below −

This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line

The following example shows the usage of the Python File read() method. Here, we are opening a file "foo.txt" in read mode (r). Then all the contents of file (upto EOF) are read using this method.

# Open a file
fo = open("foo.txt", "r")
print("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

line = fo.read()
print("Read Line: %s" % (line))

# Close opened file
fo.close()

When we run above program, it produces following result −

Name of the file:  foo.txt
Read Line: This is 1st line
This is 2nd line
This is 3rd line
This is 4th line
This is 5th line 

Example

If we pass a certain number of bytes as an optional parameter to the method, only the specified bytes are read from the file.

# Open a file
fo = open("foo.txt", "r")
print("Name of the file: ", fo.name)

line = fo.read(5)
print("Read Line: ", line)

# Close opened file
fo.close()

Let us compile and run the given program, to produce the output as follows −

Name of the file:  foo.txt
Read Line:  This 

Example

Read and Write operations can be done in a file one after the other. The read() method is called to check whether the contents added to a file using the write() method are reflected in the current file or not.

In the following example, we are trying to write content into a file using the write() method and we close the file. Then, this file is opened again in the read (r) mode, and file contents are read using the read() method.

# Open a file in write mode
fo = open("foo.txt", "w")
print("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

# Write a new line
fo.write("This is the new line")

# Close opened file
fo.close()

# Open the file again in read mode
fo = open("foo.txt", "r")

line = fo.read()
print("File Contents:", line)

# Close opened file again
fo.close()

On executing the program above, the output is displayed on the terminal as follows −

Name of the file:  foo.txt
File Contents: This is the new line
python_file_methods.htm
Advertisements