Skip to content
C

Multi-Exception File Reader

Mediumpython

Write a program that saves some text to a file called existing.txt, then attempts to open and read a given filename — handling FileNotFoundError with one message and any other exception with a generic message, using a finally block that always prints a completion message.

What the problem means: exercise a full try/except/except/finally structure where the outcome depends on whether the filename you're asked to read actually exists.

Approach: write the given content into existing.txt first. Then try to open and read the given filename — if it's existing.txt, the read succeeds; for any other name, it raises FileNotFoundError. Either way, the finally block always runs last.

Input: Two lines: the text content to save into existing.txt, then the filename to attempt reading.

Output: The file's content (if the given filename was found and read), or File not found. — followed on the next line by Read attempt complete either way.

Input (stdin)

Output

Run your code to see output here...