The first line of the with statement opens the file and assigns it to myfile, then we can iterate over each line in the file. When we are done, we simply stop indenting and let Python take care of closing the file and cleaning up.
A word of caution: once you read all the lines from a file, it is "spent"; that is to say, if we tried to read from myfile again in the example above, it would not work. We would have to open the file again to read again.
In Python, we can call the open function to open files before we can use them and the close function to close them when we are done with them. As you might expect, once a file is opened it becomes a Python object just like all other data. Table 11.3.2 shows the functions and methods that can be used to open and close files.