(原创)Python文件与文件系统系列(5)——stat模块

时间:2021-07-08 21:46:14

  stat模块中定义了许多的常量和函数,可以帮助解释 os.stat()、os.fstat()、os.lstat()等函数返回的 st_result 类型的对象。

  通常使用 os.path.is*() 这类函数来测试一个文件的类型,这些方法对同一个文件进行多次测试时,stat()系统调用都是不可避免的开销。同时,有些信息是os.path.is*() 这类函数无法提供的,例如检测是否是块设备、字符设备等。

  此时就可以使用 stat 模块及stat模块中的诸多功能,下面是一个例子:

import os, sys
import stat def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file''' for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname).st_mode
if stat.S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif stat.S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname def visitfile(file):
print 'visiting', file if __name__ == '__main__':
walktree(sys.argv[1], visitfile)

   该例子递归遍历命令行参数所指定的目录中的所有普通文件。

例:上面脚本的执行结果

# python stat_test.py data_structure/
visiting data_structure/a.out
visiting data_structure/biThrTree.h
visiting data_structure/biThrTree.c

stat 模块定义的用来测试文件类型的函数包括

stat.S_ISDIR(mode)
  判断文件是不是一个目录。
stat.S_ISCHR(mode)
  判断文件是不是一个字符型设备。
stat.S_ISBLK(mode)

  判断文件是不是一个块设备。

stat.S_ISREG(mode)

  判断mode是不是来自一个普通文件。

stat.S_ISFIFO(mode)

  判断mode是不是来自一个FIFO(如:具名管道)

stat.S_ISLNK(mode)

  判断mode是不是来自一个符号链接。

stat.S_ISSOCK(mode)

  判断mode是不是来自一个套接字。

  上面的这些函数,除了 S_ISDIR 和 S_ISREG,其他都只在 Unix 环境下才有效。

stat.S_IMODE(mode)
  返回 mode 中可以被 os.chmod() 函数设置的部分,在Unix平台上,包括:权限位、sticky bits,set-group-id位、set-uid-bit等。
stat.S_IFMT(mode)  
  返回 mode 中描述文件类型(可以被S_IS*()函数使用)的部分。
 

All the variables below are simply symbolic indexes into the 10-tuple returned by os.stat()os.fstat() or os.lstat().

stat.ST_MODE

Inode protection mode.

stat.ST_INO

Inode number.

stat.ST_DEV

Device inode resides on.

stat.ST_NLINK

Number of links to the inode.

stat.ST_UID

User id of the owner.

stat.ST_GID

Group id of the owner.

stat.ST_SIZE

Size in bytes of a plain file; amount of data waiting on some special files.

stat.ST_ATIME

Time of last access.

stat.ST_MTIME

Time of last modification.

stat.ST_CTIME

The “ctime” as reported by the operating system. On some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time (see platform documentation for details).

The interpretation of “file size” changes according to the file type. For plain files this is the size of the file in bytes. For FIFOs and sockets under most flavors of Unix (including Linux in particular), the “size” is the number of bytes waiting to be read at the time of the call to os.stat()os.fstat(), or os.lstat(); this can sometimes be useful, especially for polling one of these special files after a non-blocking open. The meaning of the size field for other character and block devices varies more, depending on the implementation of the underlying system call.

The variables below define the flags used in the ST_MODE field.

Use of the functions above is more portable than use of the first set of flags:

stat.S_IFSOCK

Socket.

stat.S_IFLNK

Symbolic link.

stat.S_IFREG

Regular file.

stat.S_IFBLK

Block device.

stat.S_IFDIR

Directory.

stat.S_IFCHR

Character device.

stat.S_IFIFO

FIFO.

The following flags can also be used in the mode argument of os.chmod():

stat.S_ISUID

Set UID bit.

stat.S_ISGID

Set-group-ID bit. This bit has several special uses. For a directory it indicates that BSD semantics is to be used for that directory: files created there inherit their group ID from the directory, not from the effective group ID of the creating process, and directories created there will also get the S_ISGID bit set. For a file that does not have the group execution bit (S_IXGRP) set, the set-group-ID bit indicates mandatory file/record locking (see also S_ENFMT).

stat.S_ISVTX

Sticky bit. When this bit is set on a directory it means that a file in that directory can be renamed or deleted only by the owner of the file, by the owner of the directory, or by a privileged process.

stat.S_IRWXU

Mask for file owner permissions.

stat.S_IRUSR

Owner has read permission.

stat.S_IWUSR

Owner has write permission.

stat.S_IXUSR

Owner has execute permission.

stat.S_IRWXG

Mask for group permissions.

stat.S_IRGRP

Group has read permission.

stat.S_IWGRP

Group has write permission.

stat.S_IXGRP

Group has execute permission.

stat.S_IRWXO

Mask for permissions for others (not in group).

stat.S_IROTH

Others have read permission.

stat.S_IWOTH

Others have write permission.

stat.S_IXOTH

Others have execute permission.

stat.S_ENFMT

System V file locking enforcement. This flag is shared with S_ISGID: file/record locking is enforced on files that do not have the group execution bit (S_IXGRP) set.

stat.S_IREAD

Unix V7 synonym for S_IRUSR.

stat.S_IWRITE

Unix V7 synonym for S_IWUSR.

stat.S_IEXEC

Unix V7 synonym for S_IXUSR.

The following flags can be used in the flags argument of os.chflags():

stat.UF_NODUMP

Do not dump the file.

stat.UF_IMMUTABLE

The file may not be changed.

stat.UF_APPEND

The file may only be appended to.

stat.UF_OPAQUE

The directory is opaque when viewed through a union stack.

stat.UF_NOUNLINK

The file may not be renamed or deleted.

stat.UF_COMPRESSED

The file is stored compressed (Mac OS X 10.6+).

stat.UF_HIDDEN

The file should not be displayed in a GUI (Mac OS X 10.5+).

stat.SF_ARCHIVED

The file may be archived.

stat.SF_IMMUTABLE

The file may not be changed.

stat.SF_APPEND

The file may only be appended to.

stat.SF_NOUNLINK

The file may not be renamed or deleted.

stat.SF_SNAPSHOT

The file is a snapshot file.

See the *BSD or Mac OS systems man page chflags(2) for more information.