Read all the lines into a list


There are rare cases when you need the whole content of a file in the memory and you cannot process it line by line. In those rare cases we have several options. readlines will read the whole content into a list converting each line from the file to be an element in the list.

Beware though, if the file is too big, it might not fit in the free memory of the computer.


examples/files/read_full_file.py
filename = 'examples/files/numbers.txt'

with open(filename, 'r') as fh:
    lines_list = fh.readlines()   # reads all the lines into a list

# print number of lines
print(len(lines_list))

for line in lines_list:
    print(line, end="")