Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Open and read file (easy but not recommended)

In some code you will encounter the following way of opening files. This was used before "with" was added to the language. It is not a recommended way of opening a file as you might easily forget to call "close" and that might cause trouble. For example you might loose data. Don't do that.

I am showing this as the first example, because it is easuer to understand.

filename = 'examples/files/numbers.txt'

fh = open(filename, 'r')
for line in fh:
    print(line)
fh.close()