Reputation: 17
In the Python code below, I created a function and pass a parameter called the dir which stands for the directory. After that, I used the try and except statements, when the function runs the try statement should open the directory passed as an argument, and if there is an error it should go to the except statement and run it.
So my issue starts at the except statement. In the except statement, I defined a variable to find the error and there is three condition to output the error that has been occurred.
But when I run this code, I get an error like: FileNotFoundError: [Errno 2] No such file or directory: "'/etc/passwd', 'w'" I was wondering if someone could help me solve the error so my code run perfectly.
def open_dir(dir): #Open directory
try: #Try if the directory has no errors.
return open(dir)
except: #except return the error occurred.
directory = open(dir)
if directory == FileNotFoundError:
return "[Errno 2] No such file or directory: 'directory'"
elif directory == PermissionError:
return "[Errno 13] Permission denied: '/et3]c/passwd'"
elif directory == IsADirectoryError:
return "[E'/home'rrno 21] Is a directory: '/home'"
print(open_dir("doc.docx"))
print(open_dir("'/etc/passwd', 'w'"))
print(open_dir('/home'))
Upvotes: 0
Views: 727
Reputation: 3
try this code.
def open_dir(dir, mode="r"): # Open directory
try: # Try if the directory has no errors.
return open(dir, mode=mode)
except Exception as e:
if e is FileNotFoundError:
return "[Errno 2] No such file or directory: 'directory'"
elif e is PermissionError:
return "[Errno 13] Permission denied: '/et3]c/passwd'"
elif e is IsADirectoryError:
return "[E'/home'rrno 21] Is a directory: '/home'"
print(open_dir("doc.docx"))
print(open_dir("'/etc/passwd', 'w'"))
print(open_dir('/home'))
Upvotes: 0
Reputation: 41
The except
block should catch specific exception instead of catching all exceptions. and Another thing is; instead of checking if the variable directory
is equal to the exception object, you should check if it is an instance of that particular exception. This is because When you catch an exception using an except
statement, the exception object is assigned to a variable so check if variable is an instance of the exception class.
Try this one:
def open_dir(dir):
try:
return open(dir)
except FileNotFoundError as e:
return f"[Errno 2] No such file or directory: '{e.filename}'"
except PermissionError as e:
return f"[Errno 13] Permission denied: '{e.filename}'"
except IsADirectoryError as e:
return f"[Errno 21] Is a directory: '{e.filename}'"
Upvotes: 0
Reputation: 968
def open_dir(dir): #Open directory
try: #Try if the directory has no errors.
return open(dir)
except FileNotFoundError: #except return the error occurred.
return "[Errno 2] No such file or directory: 'directory'"
except PermissionError:
return "[Errno 13] Permission denied: '/et3]c/passwd'"
except IsADirectoryError:
return "[E'/home'rrno 21] Is a directory: '/home'"
This should work as you desire
The problem you have is that when you do open(dir)
and it doesn't work, you go in the except fragment of your code
but in the except fragment of your code, you do open(dir)
, which re-throws an exception since it's the reason you are in the except statement.
When you do a try except, if somethings goes wrong in the "try
", it raises an exception that you can catch in the "except
" fragment, and this is where you compare the exception (with for instance except FileNotFoundError
which checks if the exception is a FileNotFoundError
)
Upvotes: 1