Python os.statvfs() Method



The Python method os.statvfs() perform a statvfs system call on the given path. It is used to retrieve the status of a file system.

When we call the os.statvfs(), it returns a statvfs_result object. This object contains various attributes that represent the status of the file system.

Syntax

Syntax of Python os.statvfs() method is as follows −

os.statvfs(path)

Parameters

The Python os.statvfs() method accepts a single parameter −

  • path − This is the path, whose statvfs information is required.

Return Value

The Python os.statvfs() methods returns a statvfs_result object that contains the below attributes −

  • f_bsize − preferred file system block size.

  • f_frsize − fundamental file system block size.

  • f_blocks − total number of blocks in the filesystem.

  • f_bfree − total number of free blocks.

  • f_bavail − free blocks available to non-super user.

  • f_files − total number of file nodes.

  • f_ffree − total number of free file nodes.

  • f_favail − free nodes available to non-super user.

  • f_flag − system dependent.

  • f_namemax − maximum file name length.

Example

The following example shows the usage of statvfs() method. Here, we are displaying statvfs information of the given file.

import os, sys

# showing statvfs information of file "a1.py"
stinfo = os.statvfs("atty.py")

print (stinfo)

When we run above program, it produces following result −

os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=6276673,
 f_bfree=2780772, f_bavail=2455601, f_files=1605632, f_ffree=1374428,
 f_favail=1374428, f_flag=4096, f_namemax=255)

Example

In this example, we are accessing various information related to filesystem using the attributes of statvfs_result object.

import os

# file
fpath = "atty.py"

# Retrieve filesystem info
statsinfo = os.statvfs(fpath)

# Display filesystem info
print(f"File system block size: {statsinfo.f_bsize}")
print(f"Fragment size: {statsinfo.f_frsize}")
print(f"Total number of blocks: {statsinfo.f_blocks}")
print(f"Total number of free blocks: {statsinfo.f_bfree}")

On executing the above program, it will display the following output −

File system block size: 4096
Fragment size: 4096
Total number of blocks: 6276673
Total number of free blocks: 2780767
python_files_io.htm
Advertisements