The use of errno modules in Py2 and Py3

Source: Internet
Author: User

The errno module defines some symbolic error codes, such as enoent ("not Found") and Eperm ("No Permissions").

If you need to distinguish between different error codes, you can use the symbol name.

In Py2, the IOError exception provides a tuple containing two values, which are error codes and error messages, as follows:

errno
try:
    fp = open (' no.such.file ')
ioerror, (Error, MSG):
    Error = = Errno. Enoent:
        ' No such file '
    error = = errno. Eperm:
        ' permission denied '
    else:
        msg

Or:

errno
try:
    fp = open (' no.such.file ')
ioerror, E:
    E.errno = = errno. Enoent:
        ' No such file '
    E.errno = = errno. Eperm:
        ' permission denied '
    else:
        Error


The exception capture method in Py3 is slightly different, the exception instance contains the error code, the code is as follows:

errno
try:
    fp = open (' no.such.file ')
e:
    E.errno = = errno. Enoent:
        print(' No such file ')
    E.errno = = errno. Eperm:
        print(' Permission denied ')
    else:
        print(e)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.