【python标准库】系统错误码errno

文章目录

    • python
    • C语言

python

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

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

其输出如下表

1 EPERM Operation not permitted
2 ENOENT No such file or directory
3 ESRCH No such process
4 EINTR Interrupted function call
5 EIO Input/output error
6 ENXIO No such device or address
7 E2BIG Arg list too long
8 ENOEXEC Exec format error
9 EBADF Bad file descriptor
10 ECHILD No child processes
11 EAGAIN Resource temporarily unavailable
12 ENOMEM Not enough space
13 EACCES Permission denied
14 EFAULT Bad address
16 EBUSY Resource device
17 EEXIST Fileexists
18 EXDEV Improper link
19 ENODEV No such device
20 ENOTDIR Not a directory
21 EISDIR Is a directory
22 EINVAL Invalid argument
23 ENFILE Too many open files in system
24 EMFILE Too many open files
25 ENOTTY Inappropriate I/O control operation
27 EFBIG File too large
28 ENOSPC No space left on device
29 ESPIPE Invalid seek
30 EROFS Read-only file system
31 EMLINK Too many links
32 EPIPE Broken pipe
33 EDOM Domain error
34 ERANGE Result too large
36 EDEADLOCK Resource deadlock avoided
38 ENAMETOOLONG Filename too long
39 ENOLCK No locks available
40 ENOSYS Function not implemented
41 ENOTEMPTY Directory not empty
42 EILSEQ Illegal byte sequence
104 EBADMSG bad message
105 ECANCELED operation canceled
111 EIDRM identifier removed
120 ENODATA no message available
121 ENOLINK no link
122 ENOMSG no message
124 ENOSR no stream resources
125 ENOSTR not a stream
127 ENOTRECOVERABLE state not recoverable
129 ENOTSUP not supported
132 EOVERFLOW value too large
133 EOWNERDEAD owner dead
134 EPROTO protocol error
137 ETIME stream timeout
139 ETXTBSY text file busy

C语言

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

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

#include
#include
#include

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

你可能感兴趣的:(#,Python标准库,python,开发语言,后端,C语言,错误码)