Python os.strerror() Method
-
Syntax of Python
os.strerror()
Method -
Example 1: Use the
os.strerror()
Method in Python -
Example 2: Use the
os.strerror()
Method to Print First 20 Errors -
Example 3: Use the
os.strerror()
Method to Check for an Invalid Error Code

Python os.strerror()
method is an efficient way of getting an error message corresponding to the input error code. An OSError
exception is raised when an invalid, or inaccessible file path is accessed or the specific operating system does not accept the arguments.
Syntax of Python os.strerror()
Method
os.strerror(code)
Parameters
code |
An integer value representing an error code. |
Return
The return type of this method is a string representing an error message corresponding to the input error code.
Example 1: Use the os.strerror()
Method in Python
import os
error_code = 1
error = os.strerror(error_code)
print(
"The error message corresponding to the input error code % d is:" % error_code,
error,
)
error_code = 5
error = os.strerror(error_code)
print(
"The error message corresponding to the input error code % d is:" % error_code,
error,
)
Output:
The error message corresponding to the input error code 1 is: Operation not permitted
The error message corresponding to the input error code 5 is: Input/output error
This method is useful when an error code is shown as output, and you need to figure out what the error is related to.
Example 2: Use the os.strerror()
Method to Print First 20 Errors
import os
error_num = 20
for i in range(1, error_num + 1):
error = os.strerror(i)
print("The error message corresponding to the input error code % d is:" % i, error)
Output:
The error message corresponding to the input error code 1 is: Operation not permitted
The error message corresponding to the input error code 2 is: No such file or directory
The error message corresponding to the input error code 3 is: No such process
The error message corresponding to the input error code 4 is: Interrupted system call
The error message corresponding to the input error code 5 is: Input/output error
The error message corresponding to the input error code 6 is: No such device or address
The error message corresponding to the input error code 7 is: Argument list too long
The error message corresponding to the input error code 8 is: Exec format error
The error message corresponding to the input error code 9 is: Bad file descriptor
The error message corresponding to the input error code 10 is: No child processes
The error message corresponding to the input error code 11 is: Resource temporarily unavailable
The error message corresponding to the input error code 12 is: Cannot allocate memory
The error message corresponding to the input error code 13 is: Permission denied
The error message corresponding to the input error code 14 is: Bad address
The error message corresponding to the input error code 15 is: Block device required
The error message corresponding to the input error code 16 is: Device or resource busy
The error message corresponding to the input error code 17 is: File exists
The error message corresponding to the input error code 18 is: Invalid cross-device link
The error message corresponding to the input error code 19 is: No such device
The error message corresponding to the input error code 20 is: Not a directory
The range of valid error codes might depend on the OS being used.
Example 3: Use the os.strerror()
Method to Check for an Invalid Error Code
import os
error_code = 200
error = os.strerror(error_code)
print("Error message corresponding to error code % d:" % error_code, error)
error_code = 300
error = os.strerror(error_code)
print("Error message corresponding to error code % d:" % error_code, error)
Output:
Error message corresponding to error code 200: Unknown error 200
Error message corresponding to error code 300: Unknown error 300
On some platforms, strerror()
might return NULL
when we input an unknown error code.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn