LearnFrenzy
Sign in Take a free test

File Handling · Find the Error · Question 4

Error: writing a number to a text file Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
import os, tempfile
os.chdir(tempfile.mkdtemp())

total = 450
with open("total.txt", "w") as f:
    f.write(total)
  • A It writes 450
  • B ValueError
  • C TypeError: write() needs a str; write str(total) or an f-string Correct
  • D It writes nothing
Correct Answer: Option C

Python reports "write() argument must be str, not int".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links