Python 中的标准 errno 系统符号
pythonserver side programmingprogramming更新于 2025/7/31 11:56:17
每种编程语言都有错误处理机制,其中一些错误已经编码到编译器中。在 Python 中,我们喜欢与一些标准的预定错误代码相关联的错误。在本文中,我们将了解如何获取内置的错误编号以及错误代码。然后举一个例子来说明如何使用错误代码。
错误代码
在此程序中,仅列出内置的错误编号和错误代码。纪念我们使用错误号模块和 OS 模块。
示例
import errno import os for i in sorted(errno.errorcode): print(i,':',os.strerror(i))
输出
运行上述代码得到以下结果 −
1 : Operation not permitted 2 : No such file or directory 3 : No such process 4 : Interrupted function call ………… ………..
我们在此演示如何提出和使用某个区域。我们以"无此文件"错误为例。
示例
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")
输出
运行上述代码得到以下结果 −
No such file or directory File to be printed no found
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

