Python os.fstat() Method
os.fstat() Overview
The os.fstat() method is used to return the status of the file descriptor fd, similar to stat().
Available on Unix, Windows.
The structure returned by the fstat method:
- st_dev: Device Information
- st_ino: The i-node value of the file
- st_mode: A mask of file information, containing information about the file’s permissions, information about the file’s type (whether it is a normal file or a pipeline file, or some other file type)
- st_nlink: Number of hard links
- st_uid: UserID
- st_gid: User Group ID
- st_rdev: Device ID (if file is specified)
- st_size: File size, in byte
- st_blksize: System I/O Block Size
- st_blocks: How many 512 byte blocks the file is made up of
- st_atime: File last access time
- st_mtime: File last modified time
- st_ctime: Modification time of the file status information (not the modification time of the file content)
os.fstat() Syntax
The fstat() method syntax format is as follows:
os.fstat(fd)
os.fstat() Parameters
- fd – Descriptor of the file.
os.fstat() Return Value
Returns the status of the file descriptor fd.
os.fstat() Demo
The following example demonstrates the use of the fstat() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Get tuple
info = os.fstat(fd)
print ("File Information :", info)
# Get file uid
print ("File UID :%d" % info.st_uid)
# Get file gid
print ("File GID :%d" % info.st_gid)
# Close file
os.close( fd)
The output of executing the above program is:
File Information : (33261, 3753776L, 103L, 1, 0, 0,
102L, 1238783197, 1238786767, 1238786767)
File UID :0
File GID :0
os.fstat() Demo2
Use of os.fstat() method to get the status of a file descriptor:
# Python program to explain os.fstat() method in apidemos.com
# importing os module
import os
# Path
path = "/home / ihritik / Desktop / file1.txt"
# open the file represented by
# the above given path and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
# Get the status of the
# file descriptor using
# os.fstat() method
status = os.fstat(fd)
# Print the status of
# the file descriptor
print(status)
# close the file descriptor
os.close(fd)
Output:
os.stat_result(st_mode=33188, st_ino=801111, st_dev=2056, st_nlink=1, st_uid=1000,
st_gid=1000, st_size=6550, st_atime=1560377051, st_mtime=1560377051, st_ctime=1560377051)