Tracking reads through the end of a file

A scanner stores numeric tags in tags.txt. The file contains exactly the nine characters represented by the Python string literal "23\n57\n81\n"; each \n represents one newline character.


The file position is the location where the next read begins. Run the following Python code mentally, assuming all operations succeed:


Python
with open("tags.txt", "r", encoding="utf-8") as f:
    a = f.read(2)
    b = f.readline()
    c = f.read(4)
    d = f.read()
    e = f.read()
    f.seek(0)
    g = f.readline()


Find the number of characters in b, the number of characters in e, and the total number of characters returned across a, b, c, d, e and g. Count repeated characters again when they are returned again. Give each answer as an exact integer in characters.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More File Input/Output Questions: