os.stat ) )函数提供各种状态信息,如文件或文件描述符(file descriptor )的权限、大小、所属的用户和组以及修改时间。 此函数实际上通过调用操作系统的系统调用stat ()来实现功能,并支持Linux的stat命令。 函数的返回值是os.stat_result对象。
操作系统. stat (函数
下面的代码来自Python的官方网页。
导入操作系统
Satinfo=OS.stat(somefile.txt ) )。
statinfo
OS.stat_result(ST_mode=33188,st_ino=7876932,st_dev=234881026,
st_nlink=1,st_uid=501,st_gid=501,st_size=264,st_atime=1297230295,
st_mtime=1297230027,st_ctime=1297230027 )
statinfo.st_size
264
os.stat ) )函数还可以接受symbolic link file作为条目。 请看下面的代码。 c.txt是a.txt的软连接。
操作系统. stat (c.txt ) ) )。
OS.stat_result(ST_mode=33204,st_ino=17693157,st_dev=64768,st_nlink=1,
st_uid=1000,st_gid=1000,st_size=0,st_atime=1560940275,
st_mtime=1560940275,st_ctime=1560940275 )
操作系统. lstat (函数
如果要查询symbolic link file本身的信息,可以使用名为os.lstat (的非常相似的函数。 请注意函数名称中增加了一个l字符。
操作系统. lstat (c.txt ) )。
OS.stat_result(ST_mode=41471,st_ino=17693175,st_dev=64768,st_nlink=1,
st_uid=1000,st_gid=1000,st_size=5,st_atime=1560940339,
st_mtime=1560940338,st_ctime=1560940338 )
知道区别了吗? st_size不是。
操作系统. fstat (函数
还有一个非常相似的函数os.fstat ()。 此函数的输入是打开文件的文件编号。
FP=open(a.txt )、r ) )
操作系统. fstat (FP.fileno ) )
OS.stat_result(ST_mode=33204,st_ino=17693157,st_dev=64768,st_nlink=1,
st_uid=1000,st_gid=1000,st_size=0,st_atime=1560940275,
st_mtime=1560940275,st_ctime=1560940275 )
os.stat_result对象属性
以上三个stat函数的返回值都是os.stat_result对象。 需要查看此对象具有哪些属性。
st_mode,模式信息。 该信息决定pathname是对应于目录、通常的文件、还是对应于设备文件等,还包括对文件的访问权。 要分析此信息,需要stat模块的接口。
st_ino与操作系统相关联,在Linux平台上为inode编号,在Win系统上为file index。
st_dev,文件所在的设备ID。
st_nlink,hard link硬链接的数量。 (了解Linux系统的硬链接和软链接)
st_UID,文件所有者的uid。
st_gid,文件所有者的GID。
st_size、文件大小、in bytes。
st_atime,最近的访问时间,单位秒。
st_mtime,上次修改时间,单位秒。
st_ctime,此属性与操作系统平台相关。 在Linux平台上,此时间是上次修改文件元数据的时间,而在Win平台上创建文件的时间。 单位秒。
有关atime、mtime和ctime的详细信息,请参见atime、mtime和ctime。
导入操作系统
fs=OS.stat(test.txt ) )。
fs
操作系统. stat _ result (ST _ mode=33204,
st_ino=33656274, st_dev=64768,st_nlink=1, st_uid=1000, st_gid=1000,
stime=1561119755, st_ctime=1561119755)
>>> fs.st_mode
33204
>>> fs.st_ino
33656274
>>> fs.st_dev
64768
>>> fs.st_nlink
1
>>> fs.st_uid
1000
>>> fs.st_gid
1000
>>> fs.st_size
11
>>> fs.st_atime
1561119758.582074
>>> fs.st_mtime
1561119755.6539743
>>> fs.st_ctime
1561119755.6539743
>>> dir(fs)
['__add__', '__class__', '__contains__', '__delattr__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__getnewargs__',
'__gt__', '__hash__', '__init__', '__init_subclass__',
'__iter__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmul__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', 'count', 'index',
'n_fields', 'n_sequence_fields', 'n_unnamed_fields',
'st_atime', 'st_atime_ns', 'st_blksize', 'st_blocks',
'st_ctime', 'st_ctime_ns', 'st_dev', 'st_gid', 'st_ino',
'st_mode', 'st_mtime', 'st_mtime_ns', 'st_nlink',
'st_rdev', 'st_size', 'st_uid']
os.stat()函数的更多用法
先来看一下os.stat()函数的docstring:
>>> help(os.stat)
Help on built-in function stat in module nt:
stat(path, *, dir_fd=None, follow_symlinks=True)
Perform a stat system call on the given path.
path
Path to be examined; can be string, bytes, a path-like object or
open-file-descriptor int.
dir_fd
If not None, it should be a file descriptor open to a directory,
and path should be a relative string; path will then be relative to
that directory.
follow_symlinks
If False, and the last element of the path is a symbolic link,
stat will examine the symbolic link itself instead of the file
the link points to.
dir_fd and follow_symlinks may not be implemented
on your platform. If they are unavailable, using them will raise a
NotImplementedError.
It's an error to use dir_fd or follow_symlinks when specifying path as
an open file descriptor.
看得出来,上面的信息是从Win系统中打印出来的。
dir_fd参数,默认为None,如果不是None,path参数就是一个相对位置。
follow_symlinks,默认为True,表示follow symbolic link file,上面已经有示例代码了。
注意函数参数中的*符号,它表示dir_fd和follow_symlinks这两个参数在使用的时候,必须采用key=value的形式。这个函数参数定义的细节,请参考:函数参数定义中独立的*符号,什么意思?
-- EOF --