Account
Categories

Reading Files


Definition:

In Python, when you use the read() method in a program, it allows you to read the content of a file.

This method copies the data from the source file into the program.

You can see or display the data, but you cannot change the original content of the file.

Note:

Whenever you use the read() method,you should also use the close() method to close the file after reading it.

Syntax:

file_handle = open("file_name", "r")
file_handle.read()
file_handle.close()

Example:

f = open("file1.txt", "r")
content = f.read()
print(content)
f.close()

Output: