Chapters
Python114 chapters

Exercises

File Handling

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Write one line to notes.txt and read it back, using with both times.

Python
# write then read
Exercise 2

Open in a mode that refuses to overwrite an existing file, and report the refusal.

Python
with open("once.txt", "w", encoding="utf-8") as f:
    f.write("created")

# now try again without overwriting
Exercise 3

Read a file that is not there, printing a message instead of crashing.

Python
# read not-there.txt safely