Python3 os.fstat() Method
Python3 OS File/Directory Methods
Overview
The os.fstat()
method is used to return the status of a file descriptor fd
, similar to stat()
.
Available on Unix and Windows.
The structure returned by the fstat
method includes:
-
st_dev: Device information
-
st_ino: File's i-node value
-
st_mode: A mask of file information, including file permissions and type (whether it's a regular file, pipe, or other file types)
-
st_nlink: Number of hard links
-
st_uid: User ID
-
st_gid: Group ID
-
st_rdev: Device ID (if specified file)
-
st_size: File size in bytes
-
st_blksize: System I/O block size
-
st_blocks: Number of 512-byte blocks the file consists of
-
st_atime: Most recent access time of the file
-
st_mtime: Most recent modification time of the file
-
st_ctime: Modification time of the file's status information (not the file content modification time)
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 a file
fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT)
# Get the 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 the file
os.close(fd)
Executing the above program outputs:
File Information : (33261, 3753776, 103, 1, 0, 0,
102, 1238783197, 1238786767, 1238786767)
File UID :0
File GID :0