python系统错误码errno

文章目录

python

即标准的errno系统符号,与C语言中的errno.h相同,其数字代码所对应的文字描述可通过os.strerror来获取。

>>> for i in np.sort(list(errno.errorcode.keys())):
...   print(i,errno.errorcode[i],os.strerror(i))
...

其输出如下表

1EPERMOperation not permitted
2ENOENTNo such file or directory
3ESRCHNo such process
4EINTRInterrupted function call
5EIOInput/output error
6ENXIONo such device or address
7E2BIGArg list too long
8ENOEXECExec format error
9EBADFBad file descriptor
10ECHILDNo child processes
11EAGAINResource temporarily unavailable
12ENOMEMNot enough space
13EACCESPermission denied
14EFAULTBad address
16EBUSYResource device
17EEXISTFileexists
18EXDEVImproper link
19ENODEVNo such device
20ENOTDIRNot a directory
21EISDIRIs a directory
22EINVALInvalid argument
23ENFILEToo many open files in system
24EMFILEToo many open files
25ENOTTYInappropriate I/O control operation
27EFBIGFile too large
28ENOSPCNo space left on device
29ESPIPEInvalid seek
30EROFSRead-only file system
31EMLINKToo many links
32EPIPEBroken pipe
33EDOMDomain error
34ERANGEResult too large
36EDEADLOCKResource deadlock avoided
38ENAMETOOLONGFilename too long
39ENOLCKNo locks available
40ENOSYSFunction not implemented
41ENOTEMPTYDirectory not empty
42EILSEQIllegal byte sequence
104EBADMSGbad message
105ECANCELEDoperation canceled
111EIDRMidentifier removed
120ENODATAno message available
121ENOLINKno link
122ENOMSGno message
124ENOSRno stream resources
125ENOSTRnot a stream
127ENOTRECOVERABLEstate not recoverable
129ENOTSUPnot supported
132EOVERFLOWvalue too large
133EOWNERDEADowner dead
134EPROTOprotocol error
137ETIMEstream timeout
139ETXTBSYtext file busy

C语言

在C语言中,errno.h定义了整数变量errno,用于表明错误类型,程序启动时,errno为零。其实,Python中对错误码的解读也来自于此。

如果该值不为0,说明发生了错误,操作系统会定义各种错误码所对应的错误类型,例如2表示未找到文件或文件夹等,而错误号所对应的错误类型被封装在string.h中,可通过函数strerror()来搜索。

#include<stdio.h>
#include<errno.h>
#include<string.h>

int main(){
	for(int i=0; i<10; i++)
		printf("%d:%s\n",i,strerror(i));
	return 0;
}

编译运行

>gcc testErr.c
>a.exe
0:No error
1:Operation not permitted
2:No such file or directory
3:No such process
4:Interrupted function call
5:Input/output error
6:No such device or address
7:Arg list too long
8:Exec format error
9:Bad file descriptor

所以我们测试一下,若调用一个不存在的文件或文件夹,其main函数改为

int main(){
	FILE *fp;
	fp = fopen("test.txt","r");
	if (fp==NULL)
		printf("%d:%s",errno, strerror(errno));
	return 0;
}
>gcc errFile.c
>a.exe
2:No such file or directory
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微小冷

请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值