O'Reilly logo
  • Longxiang Lyu thinks this is interesting:

10-1. Learning Python:

From

Cover of Python Crash Course

Note

filename = 'learning_python.txt'

with open(filename) as file_object:
print("\nprint by reading entire file")
print(file_object.read())

file_object.seek(0, 0)

print("\nprint by looping over the file object")
for line in file_object:
    print(line.rstrip())

file_object.seek(0, 0)

print("\nprint by reading into a list")
contents = file_object.readlines()

for line in contents:
print(line.rstrip())