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 using with (recommended)

  • open
  • close
  • with
filename = 'examples/files/numbers.txt'

with open(filename, 'r') as fh:   # open(filename) would be enough
    for line in fh:
        print(line)               # duplicate newlines

# close is called when we leave the 'with' context