Page 1 of 1

Re: Any OSError Codes as of now?

Posted: Mon Jun 24, 2019 3:31 pm
by rhubarbdog
They are just text versions of the constants defined in file errno.h. micropython sticks to these eg ENODEV to remain lean.

If you need a translator to convert these to english you need to write a C program

Re: Any OSError Codes as of now?

Posted: Tue Jun 25, 2019 12:28 am
by jimmo
Using regular CPython:

Code: Select all

>>> import os
>>> os.strerror(27)
'File too large'
These aren't included in MicroPython because it's just a lot of space to take up.

Re: Any OSError Codes as of now?

Posted: Mon Jan 18, 2021 6:00 am
by vicyclefive
I want to handle specific OSError codes like this:

try:
os.scandir()
except OSPermissionError as error:
# Only catch errno.EACCES, errno.EPERM
handle_permission_error()
except OSFileNotFoundError as error:
# Only catch errno.ENOENT
handle_FileNotFoundError_error()
Can this be done in python? shareit get-vidmateapk.com

Re: Any OSError Codes as of now?

Posted: Tue Jan 19, 2021 2:48 am
by jimmo
vicyclefive wrote:
Mon Jan 18, 2021 6:00 am
I want to handle specific OSError codes like this:
I think you need to do something like:

Code: Select all


EFOO = const(1)

try:
  os.scandir()
except OSError as error:
  if error.args[0] == EFOO:
    # handle foo
  elif ...

Re: Any OSError Codes as of now?

Posted: Sat Feb 13, 2021 9:29 am
by Chai3

Code: Select all

os.scandir()
doesn't throw these types of exceptions. It raises the OSError exception. It does, however, allow you to determine the type of error that occurred.

There are a large number of possible errors that could be part of the

Code: Select all

OSError
. You can use these to raise your own custom exceptions and then handle them further up the stack.

Code: Select all

class OSPermissionError(Exception):
    pass

class OSFileNotFoundError(Exception):
    pass

try:
    os.scandir()
except OSError as error:
    # Not found
    if error.errno == errno.ENOENT: 
        raise OSFileNotFoundError()
    # Permissions Error
    elif error.errno in [errno.EPERM, errno.EACCES]: 
        raise OSPermissionError()
    else:
        raise
Best regard's