16 lines
701 B
Python
16 lines
701 B
Python
# 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: ')) |