Open In App

Python | os.fstatvfs() method

Last Updated : 19 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.fstatvfs() method in Python is used to get information about the file system containing the file associated with the given file descriptor. To get file system information the method performs statvfs() system call on the path associated with the given file descriptor.

A file descriptor is small integer value that corresponds to a file or other input/output resource, such as a pipe or network socket. It is an abstract indicator of a resource and act as handle to perform various lower level I/O operations like read, write, send etc.
For Example: Standard input is usually file descriptor with value 0, standard output is usually file descriptor with value 1 and standard error is usually file descriptor with value 2.
Further files opened by the current process will get the value 3, 4, 5 an so on.

Note: os.fstatvfs() method is available on Unix platforms only.

Syntax: os.fstatvfs(fd)

Parameter:
fd: A file descriptor for which file system information is required.

Return Type: This method returns an object of class ‘os.statvfs_result’ whose attributes represents the information about the filesystem containing the file associated with the given file descriptor.
The returned os.statvfs_result object has following attributes:

  • f_bsize: It represents the file system block size
  • f_frsize: It represents the fragment size
  • f_blocks It represents the size of fs in f_frsize units
  • f_bfree: It represents the number of free blocks
  • f_bavail: It represents the number of free blocks for unprivileged users
  • f_files: It represents the number of inodes
  • f_ffree: It represents the number of free inodes
  • f_favail: It represents the number of free inodes for unprivileged users
  • f_fsid: It represents the file system ID
  • f_flag: It represents the mount flags
  • f_namemax: It represents the maximum filename length
Code: 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 
    
# 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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads