Open oldoc63 opened 1 year ago
This script also creates a file object called sonnet_doc that points to the file millay_sonnet.txt. It then reads in the first line using sonnet_doc.readline() and saves that to the variable first_line. It then saves the second line into the variable second_line and then prints it out.
Sometimes you don't want to iterate through a whole file. For that, there's a different file method, readline(), which will only read a single line at a time. If the entire document is read line by line in this way subsequent calls to .readline() will not throw an error but will start returning an empty string ("").