Python | os.strerror() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

All functions in os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

os.strerror() method in Python is used to get the error message corresponding to the error code.



Syntax: os.strerror(code)

Parameter:
code: A integer value that denotes the error code

Return Type: This method returns a string representing the error message corresponding to the specified error code.

Code #1: Use of os.strerror() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.strerror() method 
    
# importing os module 
import os
  
# Get the error message 
# corresponding to 
# error code 1
code = 1
error = os.strerror(code)
  
# Print the error message 
# corresponding to 
# error code 1
print("Error message corresponding to error code % d:" % code, error)
  
# Get the error message 
# corresponding to 
# error code 5
code = 5
error = os.strerror(code)
  
# Print the error message 
# corresponding to 
# error code 5
print("Error message corresponding to error code % d:" % code, error)

chevron_right


Output:

Error message corresponding to error code 1: Operation not permitted
Error Message corresponding to error code 5: Input/output error

Code #2: Printing first 20 errors

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.strerror() method 
    
# importing os module 
import os
  
# Get the error message 
# corresponding to the
# first 20 error codes
n = 20
  
for i in range(1, n + 1)
    error = os.strerror(code)
    print("Error code % d:" % i, error)

chevron_right


Output:

Error code 1: Operation not permitted
Error code 2: No such file or directory
Error code 3: No such process
Error code 4: Interrupted system call
Error code 5: Input/output error
Error code 6: No such device or address
Error code 7: Argument list too long
Error code 8: Exec format error
Error code 9: Bad file descriptor
Error code 10: No child processes
Error code 11: Resource temporarily unavailable
Error code 12: Cannot allocate memory
Error code 13: Permission denied
Error code 14: Bad address
Error code 15: Block device required
Error code 16: Device or resource busy
Error code 17: File exists
Error code 18: Invalid cross-device link
Error code 19: No such device
Error code 20: Not a directory

Code #3: Checking error message for an invalid error code

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.strerror() method 
    
  
# importing os module 
import os
  
# Get the error message 
# corresponding to 
# error code 200
code = 200
error = os.strerror(code)
  
# Print the error message 
# corresponding to 
# error code 200
print("Error message corresponding to error code % d:" % code, error)
  
  
# Get the error message 
# corresponding to 
# error code 300
code = 300
error = os.strerror(code)
  
# Print the error message 
# corresponding to 
# error code 300
print("Error message corresponding to error code % d:" % code, error)
  
# os.strerror() will return
# An unknown error
# in case specified code
# is invalid

chevron_right


Output:

Error message corresponding to error code 200: Unknown error 200
Error message corresponding to error code 300: Unknown error 300


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.