Standard errno system symbols in Python

PythonServer Side ProgrammingProgramming

Every programming language has a error handling mechanism in which some errors are already coded into the compiler. In Python we have love which are associated with some standard pre-determined error codes. In this article we will see how to to get the error numbers as well as error codes which are inbuilt. And then take an example of how error code can be used.

Error codes

In this program just list out the inbuilt error numbers and error codes. Memorial we use the error no module along with the OS module.

Example

 Live Demo

import errno
import os
for i in sorted(errno.errorcode):
   print(i,':',os.strerror(i))

Output

Running the above code gives us the following result −

1 : Operation not permitted
2 : No such file or directory
3 : No such process
4 : Interrupted function call
…………
………..

Here we demonstrate how how an area is raised and used. We consider the - No such file error as an example.

Example

 Live Demo

try:
   file_name = open('Data.txt')
# 2 is 'No such file or directory'
   except IOError as e:
   if e.errno == 2:
      print(e.strerror)
      print("File to be printed no found")
      # handle exception
   elif e.errno == 9:
      print(e.strerror)
      print("File will not print")

Output

Running the above code gives us the following result −

No such file or directory
File to be printed no found
raja
Published on 09-Jul-2020 13:53:16
Advertisements