Line and character counts after mixed writes

A program starts with no file named out.txt on the disk and runs the Python code below. Each newline character counts as one character.


Python
with open("out.txt", "w") as f:
    f.write("red")
    f.write("green\n")
    f.write("blue\n")
with open("out.txt", "a") as f:
    print("gold", file=f)
with open("out.txt") as f:
    lines = f.readlines()


After the code finishes, find the value of len(lines), the total number of characters stored in out.txt, and the value of len(lines[0]). Give each as a whole number.

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: