Python3 os.lstat()方法

lstat() 方法非常相似于fstat()方法,它返回包含有关文件信息的 stat_result 对象, 但不遵循符号链接。这是fstat()的别名,不支持符号链接,如在 Windows 平台。
下面是 lstat() 方法返回的结构:
  • st_dev: 包含设备文件的标识

  • st_ino: inode编号

  • st_mode: 保护

  • st_nlink: 硬链接数

  • st_uid: 所有者的用户ID

  • st_gid: 所有者的组ID

  • st_rdev: 设备ID(特殊文件)

  • st_size: 总大小,以字节为单位

  • st_blksize: 文件系统I/O的块大小

  • st_blocks: 分配的块数

  • st_atime: 上次访问时间

  • st_mtime: 最后修改时间

  • st_ctime: 状态最后一次修改的时间

语法

以下是 lstat() 方法的语法:
os.lstat(path)

参数

  • path -- 这是将返回信息的文件。

返回值

此方法返回有关文件的信息。

示例

下面的示例显示 lstat() 方法的使用。
#!/usr/bin/python3

import os, sys

# Open a file
path = "d:\\python3\\foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# Close opened file
os.close( fd )

# Now get  the touple
info = os.lstat(path)

print ("File Info :", info)

# Now get uid of the file
print ("UID of the file :%d" % info.st_uid)

# Now get gid of the file
print ("GID of the file :%d" % info.st_gid)
当我们运行上面的程序,它会产生以下结果:
File Info : os.stat_result(st_mode=33206, st_ino=281474976797706, st_dev=1017554828, st_nlink=2, st_uid=0, st_gid=0, st_size=13, st_atime=1455597777, st_mtime=1438077266, st_ctime=1455560006)
UID of the file :0
GID of the file :0

上一篇: Python3文件方法 下一篇: Python3 os文件目录的方法