From 264a89af6592b25bd9f25c3a6599f982b2af47f9 Mon Sep 17 00:00:00 2001 From: baphim <269010393+baphim@users.noreply.github.com> Date: Sun, 13 Sep 2026 09:50:30 -0400 Subject: [PATCH] feat(problem-03.py): completed problem. --- lab-02/problem-03.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lab-02/problem-03.py b/lab-02/problem-03.py index 43f99f2..7dbc2c2 100644 --- a/lab-02/problem-03.py +++ b/lab-02/problem-03.py @@ -1 +1,16 @@ -# Write a Python program that opens a file and handles a FileNotFoundError exception if the file does not exist. \ No newline at end of file +# Write a Python program that opens a file and handles a FileNotFoundError exception if the file does not exist. + +# Define open_file function to open files specified by user +def open_file(filename): + # Try to open entered filename + try: + open(filename) + except FileNotFoundError: + # Handle expected FileNotFoundError and print error message + print('That file cannot be found.') + except PermissionError: + # Handle PermissionError exception for file existing in directory used in problem-05.py + print('Insufficient permission to open file') + +#Prompt user to enter a file name to be opened +open_file(filename = input('Enter the name of the file to open: ')) \ No newline at end of file