feat(problem-03.py): completed problem.
This commit is contained in:
+16
-1
@@ -1 +1,16 @@
|
|||||||
# Write a Python program that opens a file and handles a FileNotFoundError exception if the file does not exist.
|
# 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: '))
|
||||||
Reference in New Issue
Block a user