Common Student Questions
What is f in the print_all and other functions? The f is a variable just like you had in other functions in Exercise 18, except this time it’s a file. A file in Python is kind of like an old tape drive on a mainframe or maybe a DVD player. It has a “read head,” and you can “seek” this read head around the file to positions, then work with it there. Each time you do f.seek(0) you’re moving to the start of the file. Each time you do f.readline() you’re reading a line from the file and moving the read head to right after the \n that ends that line. This will be explained more as you go on.
Why does seek(0) not set the current_line to 0? First, the seek() function is dealing in bytes, not lines. The code seek(0) moves the file to the 0 byte (first byte) in the file. Second, current_line is just a variable and has no real connection to the file at all. We are manually incrementing it.
What is +=? You know how in English I can rewrite “it is” as “it’s”? Or I can rewrite “you are” as “you’re”? In English this is called a contraction, and this is kind of like a contraction for the two operations = and +. That means x = x + y is the same as x += y.
How does readline() know where each line is? Inside readline() is code that scans each byte of the file until it finds a \n character, then stops reading the file to return what it found so far. The file f is responsible for maintaining the current position in the file after each readline() call, so that it will keep reading each line.
Why are there empty lines between the lines in the file? The readline() function returns the \n that’s in the file at the end of that line. Add a end = "" at the end of your print function calls to avoid adding double \n to every line.