Python 3 – os.fstatvfs() Method
The os.fstatvfs()
method in Python 3 returns information about the filesystem containing the specified file. It takes a file descriptor as its argument and returns a tuple containing information about the filesystem as follows:
os.fstatvfs(fd)
Here fd
is the file descriptor. The fstatvfs() method returns the following tuple:
- f_bsize: The file system block size (in bytes)
- f_frsize: The fragment size (in bytes)
- f_blocks: The size of the filesystem in blocks
- f_bfree: The number of free blocks
- f_bavail: The number of free blocks available to non-superuser
- f_files: The total number of file nodes
- f_ffree: The number of free file nodes
- f_favail: The number of free file nodes available to non-superuser
- f_flag: Mount flags
- f_namemax: Maximum length of filenames
It is important to note that the values returned by fstatvfs() method may vary depending on the platform on which it is called.
Here’s an example that demonstrates the usage of the fstatvfs() method:
import os
# get the file descriptor of the current file
fd = os.open(".", os.O_RDONLY)
# get the filesystem information using fstatvfs()
filesystem_info = os.fstatvfs(fd)
print(filesystem_info)
# close the file descriptor
os.close(fd)
Output:
posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=36533678, f_bfree=18324903, f_bavail=16297365, f_files=9243776, f_ffree=8911208, f_favail=8911208, f_flag=4096, f_namemax=255)
In this example, we opened the current directory (“.”) and got the file descriptor using os.open()
method. We then passed this file descriptor to the os.fstatvfs()
method to get information about the filesystem containing the current directory. The method returned a tuple containing various values describing the filesystem.
The os.close()
method is then used to close the file descriptor that was opened earlier.
Conclusion
The os.fstatvfs()
method in Python 3 is a useful method for obtaining information about the filesystem containing a specified file. It returns a tuple containing various values that can be used to determine the size of the filesystem, the number of free blocks and file nodes, and the maximum length of filenames. It is important to note that the values returned by fstatvfs()
method may vary depending on the platform on which it is called.