How to read entire text file in Python?

Often one might need to read the entire content of a text file (or flat file) at once in python. In this post, we showed an example of reading the whole file and reading a text file line by line. Here is another way to import the entire content of a text file.

# Open a file: file
file = open('my_text_file',mode='r')

# read all lines at once
all_of_it = file.read()

# close the file
file.close()