Learn Python 3 | Reading a Line

with open('millay_sonnet.txt') as sonnet_doc:
  first_line = sonnet_doc.readline()
  second_line = sonnet_doc.readline()

The text above is used as an example to explain how to print a file one line at a time. I’m curious, it seems like first_line and second_line would be equal to the same thing, why don’t they? Https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/reading-a-line

Because of the serial nature of reading in data as chunks (physical delimited lines) once a chunk is read in, the pointer moves to the next line, as if the first line was consumed.


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.