Change language

Python | os.strerror () method

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

os.strerror () 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: Using the os method .strerror ()

# Python program to explain the os.strerror () method

 
# import of the os module

import os

 
# Get error message
# matches
# error code 1

code = 1

error = os.strerror (code)

 
# Print error message
# matches
# error code 1

print ( "Error message corresponding to error code % d: " % code, error)

  
# Get error message
# matches
# error code 5

code = 5

error = os.strerror (code)

 
# Print error message
# matches
# error code 5

print ( "Error message corresponding to error code% d:" % code, error)

Exit :

 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

# Python program to explain the os.strerror () method

 
# import module os

import os

 
# Get an error message
# matches
# first 20 error codes

n = 20

 

for i in range ( 1 , n + 1 )

error = os.strerror (code)

print ( "Error code% d : " % i, error)

Exit :

 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: Check error message for invalid error code

# Python program to explain the os.strerror () method

 

 
# import of the os module

import os

 
# Get error message
# matches 
# error code 200

code = 200

error = os.strerror (code)

 
# Print error message
# matches
# error code 200

print ( "Error message corresponding to error code% d : " % code, error)

  

 
# Get error message
# matches
# error code 300

code = 300

error = os.strerror (code)

 
# Print error message
# matches
# error code 300

print ( "Error message corresponding to error code% d:" % code, error)

 
# os.strerror () will return
# Unknown error
# if code is specified
# is invalid

Exit:

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

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically