阅读 Python 文档 - stat 模块
Python stat 模块定义了一些帮助解读 struct stat 结构体的常量和函数。struct stat 结构体可以由 stat, fstat, lstat 这三种操作返回。
我们一般用 ls -l 查看 stat 的信息。
这些常量应该被定义在 <sys/stat.h> 头文件中,例如
S_ISDIR(mode)- 目录S_ISCHR(mode)- character special device file (for nocaching device reading)S_ISBLK(mode)- block special device file (for caching device reading)- 注:
/dev下的东东要么是 CHR 要么是 BLK
- 注:
S_ISREG(mode)- 普通文件S_ISFIFO(mode)- 进程间通信的 FIFO(也叫 named pipe).S_ISSOCK(mode)- socket, 进程间网络通信S_ISLNK(mode)- 链接。- BCDFRSL
os.path.is*() 方法其实定义了 isfile, isdir, islink 三个方法。这三个方法平时也够用。用 S_*(mode) 系列方法的好处是:
- 减少系统调用,一个调用多次检查
- 还能检测出 FIFO, sock, 等等。