Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python using the try and except keywords for returning directory errors

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'))

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading