Python3 os.fstat() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.fstat() method is used to return the status of the file descriptor fd, similar to stat().

Available on Unix and Windows.

The structure returned by the fstat method:

  • st_dev:Device information

  • st_ino:The i-node value of the file

  • st_mode:The file information mask, which includes the file's permission information and the file's type information (whether it is a regular file, a pipe file, or another file type)

  • st_nlink:Number of hard links

  • st_uid:User ID

  • st_gid:User group ID

  • st_rdev:Device ID (if the file is specified)

  • st_size:File size, in bytes

  • st_blksize:System I/O block size

  • st_blocks:The number of 512-byte blocks the file consists of

  • st_atime:The file's most recent access time

  • st_mtime:The file's most recent modification time

  • st_ctime:The modification time of the file status information (not the modification time of the file content)

Syntax

fstat()The method syntax is as follows:

os.fstat(fd)

Parameters

  • fd-- The file descriptor.

Return Value

Returns the status of the file descriptor fd.

Example

The following example demonstrates the use of the fstat() method:

#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 获取元组
info = os.fstat(fd)

print ("文件信息 :", info)

# 获取文件 uid
print ("文件 UID :%d" % info.st_uid)

# 获取文件 gid
print ("文件 GID  :%d" % info.st_gid)

# 关闭文件
os.close( fd)

The output of the above program is:

文件信息 : (33261, 3753776L, 103L, 1, 0, 0, 
            102L, 1238783197, 1238786767, 1238786767)
文件 UID :0
文件 GID :0

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions