Introduction
The very basics of text files
6. Reading files line by line
with statement and exception handling
Summary

Instruction

Great! So far, we've only read our files as a whole. In Python, it's actually pretty easy to read files line by line, too:

file = open('employees.txt', 'r')
for line in file:
  print('Employee: ' + line)
file.close()

It turns out that files are sequences in Python, which means we can use the for line in file type of loop. Naturally, you can replace line with any other name you want; it's just a temporary for-loop variable. This for loop iterates over lines in the text file. With each loop iteration, Python stores the next line from the file in the line variable.

Exercise

Open the random_numbers.txt file in read-only mode. The file contains a set of positive integers, each on a separate line.

Your task is to analyze the contents of the file and print the biggest integer to the output.

Stuck? Here's a hint!

Use for line in file:, as shown in the example. Inside the for loop, you can use int(line) to convert the line to an integer.