Python os.fstatvfs() Method
os.fstatvfs() Overview
The os.fstatvfs() method is used to return information about the file system of the file containing the file descriptor fd. Python 3.3 is equivalent to statvfs().
Available on Unix/Linux.
The structure returned by the fstatvfs method:
- f_bsize: File System Block Size
- f_frsize: Split Stack Size
- f_blocks: Total number of file system data blocks
- f_bfree: Available blocks
- f_bavail: Number of blocks available to non-super users
- f_files: Total number of file nodes
- f_ffree: Number of available file nodes
- f_favail: Number of available file nodes for non-super users
- f_fsid: File System Identification ID
- f_flag: Mount Marker
- f_namemax: Maximum file length
os.fstatvfs() Syntax
The fstatvfs() method syntax format is as follows.
os.fstatvfs(fd)
os.fstatvfs() Parameters
- fd – Descriptor of the file.
os.fstatvfs() Return Value
Returns information about the file system of the file containing the file descriptor fd.
os.fstatvfs() Demo#1
The following example demonstrates the use of the fstatvfs() method:
#!/usr/bin/python3
import os, sys
# Open file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Get tuple
info = os.fstatvfs(fd)
print ("File Information :", info)
# Get the maximum length of the file name
print ("Maximum file name length :%d" % info.f_namemax)
# Get the number of available blocks
print ("Number of available blocks :%d" % info.f_bfree)
# Close file
os.close( fd)
The output of executing the above program is:
File Information : (4096, 4096, 2621440L, 1113266L, 1113266L,
8929602L, 8764252L, 8764252L, 0, 255)
Maximum file name length :255
Number of available blocks :1113266
os.fstatvfs() Demo#2
Use of os.fstatvfs() method to get information about the filesystem containing the file associated with the given file descriptor.
# Python program to explain os.fstatvfs() method in apidemos.com
# importing os module
import os
# File path
path = "/home / ihritik / Desktop / file.txt"
# open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_WRONLY)
# get the information about the
# filesystem containing the file
# associated with the given
# file descriptor using os.fstatvfs() method
info = os.fstatvfs(fd)
# Print the information
# about the file system
print(info)
# Print the file system block size
print("File system block size:", info.f_bsize)
# Print the number of free blocks
# in the file system
print("Number of free blocks:", info.f_bfree)
# Close the file descriptor
os.close(fd)
Output:
os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=59798433, f_bfree=56521834,
f_bavail=53466807, f_files=15261696, f_ffree=14933520, f_favail=14933520, f_flag=4096,
f_namemax=255)
File system block size: 4096
Number of free blocks: 56517297