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