Python os.fstat() Method

Python File(文件) 方法Python 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:File information mask, containing file permission information and file 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: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 that make up the file

  • st_atime:Last access time of the file

  • st_mtime:Last modification time of the file

  • st_ctime: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/python
# -*- coding: UTF-8 -*-

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

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions