Python3 os.fstat() Method | Tutorial
Python3.x Python3 os.fstat() Method
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: A mask for the file information, which includes the file's permission information and file type information (whether it's 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 specified file is a device file)
- st_size: File size, in bytes
- st_blksize: Block size for system I/O
- st_blocks: Number of 512-byte blocks allocated for the file
- st_atime: Time of most recent access
- st_mtime: Time of most recent modification
- st_ctime: Time of most recent status change (not the time of modification of the file content)
Syntax
The syntax for the fstat() method 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
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Get tuple
info = os.fstat(fd)
print ("File info :", 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)
Executing the above program outputs the following result:
File info : (33261, 3753776L, 103L, 1, 0, 0, 102L, 1238783197, 1238786767, 1238786767)
File UID :0
File GID :0
YouTip