Any OSError Codes as of now?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
cahe
Posts: 1
Joined: Sun Jun 23, 2019 12:00 pm

Any OSError Codes as of now?

Post by cahe » Sun Jun 23, 2019 12:12 pm

Hi team, is there any OSError code list with translation what each code implies? As that I feel the codes are not completed yet.

Regards,
Cahe https://dltutuapp.com/tutuapp-download/ https://showbox.run/ https://kodi.software/

rhubarbdog
Posts: 166
Joined: Tue Nov 07, 2017 11:45 pm

Re: Any OSError Codes as of now?

Post by rhubarbdog » Mon Jun 24, 2019 3:31 pm

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

User avatar
jimmo
Posts: 2244
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Any OSError Codes as of now?

Post by jimmo » Tue Jun 25, 2019 12:28 am

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.

vicyclefive
Posts: 1
Joined: Sun Jan 17, 2021 7:03 pm
Contact:

Re: Any OSError Codes as of now?

Post by vicyclefive » Mon Jan 18, 2021 6:00 am

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
Last edited by vicyclefive on Thu Jan 21, 2021 6:51 pm, edited 1 time in total.

User avatar
jimmo
Posts: 2244
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Any OSError Codes as of now?

Post by jimmo » Tue Jan 19, 2021 2:48 am

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 ...

Chai3
Posts: 1
Joined: Sat Feb 06, 2021 9:44 am

Re: Any OSError Codes as of now?

Post by Chai3 » Sat Feb 13, 2021 9:29 am

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
https://gbapps.net

Post Reply