close

[Solved] How to handle OSError: [Errno 36] File name too long

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to handle OSError: [Errno 36] File name too long in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to handle OSError: [Errno 36] File name too long Error Occurs?

Today I get the following error How to handle OSError: [Errno 36] File name too long in python.

How To Solve handle OSError: [Errno 36] File name too long Error ?

  1. How To Solve handle OSError: [Errno 36] File name too long Error ?

    To Solve handle OSError: [Errno 36] File name too long Error For readability you may consider using appropriate constant from errno built-in module instead of hardcoded constant.

  2. How to handle OSError: [Errno 36] File name too long

    To Solve handle OSError: [Errno 36] File name too long Error For readability you may consider using appropriate constant from errno built-in module instead of hardcoded constant.

Solution 1

Simply check errno attribute of caught exception.

try:
    do_something()
except OSError as exc:
    if exc.errno == 36:
        handle_filename_too_long()
    else:
        raise  # re-raise previously caught exception

For readability you may consider using appropriate constant from errno built-in module instead of hardcoded constant.

Solution 2


You can specify just how you want to catch a specific error such as errno.ENAMETOOLONG:

Specific to your question…

try:
    # try stuff
except OSError as oserr:
    if oserr.errno != errno.ENAMETOOLONG:
        # ignore
    else:
        # caught...now what?

Specific to your comments…

try:
    # try stuff
except Exception as err:
    # get the name attribute from the exception class
    errname = type(err).__name__
    # get the errno attribute from the exception class
    errnum = err.errno
    if (errname == 'OSError') and (errnum == errno.ENAMETOOLONG):
        # handle specific to OSError [Errno 36]
    else if (errname == 'ExceptionNameHere' and ...:
        # handle specific to blah blah blah
    .
    .
    .
    else:
        raise # if you want to re-raise; otherwise code your ignore

This will grab all exceptions caused by errors in the try. Then it checks if the __name__ matches any specific exception and any additional conditions you want to specify.

You should know there is no getting around the except if an error is encountered unless you specific a concrete exception.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read