Python3 Os Statvfs
## Python3 os.statvfs() Method
The `os.statvfs()` method in Python is used to retrieve information and statistics about the file system containing the path specified. It performs a `statvfs()` system call on the given path, returning an object whose attributes describe the file system's capacity, block sizes, and inode limits.
---
### Syntax
```python
os.statvfs(path)
```
### Parameters
* **`path`**: A string or bytes object representing the path of any file or directory within the target file system.
### Return Value
The method returns a `statvfs_result` object (which behaves like a named tuple). Its attributes correspond to the members of the standard C `statvfs` structure:
| Attribute | Description |
| :--- | :--- |
| **`f_bsize`** | File system block size (preferred I/O block size) |
| **`f_frsize`** | Fundamental file system block size (fragment size) |
| **`f_blocks`** | Total number of blocks in the file system (in units of `f_frsize`) |
| **`f_bfree`** | Total number of free blocks |
| **`f_bavail`** | Number of free blocks available to non-privileged (non-superuser) users |
| **`f_files`** | Total number of file nodes (inodes) |
| **`f_ffree`** | Total number of free file nodes |
| **`f_favail`** | Number of free file nodes available to non-privileged users |
| **`f_fsid`** | File system ID |
| **`f_flag`** | Mount flags (system-dependent, e.g., read-only, nosuid) |
| **`f_namemax`** | Maximum filename length allowed on this file system |
---
### Code Examples
#### Example 1: Basic Usage
The following example demonstrates how to retrieve and print the raw `statvfs` information for a specific file path.
```python
#!/usr/bin/python3
import os
# Retrieve statvfs information for the file "a1.py"
stinfo = os.statvfs('a1.py')
print(stinfo)
```
**Example Output:**
```text
os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=1909350, f_bfree=1491513, f_bavail=1394521, f_files=971520, f_ffree=883302, f_favail=883302, f_fsid=16777220, f_flag=0, f_namemax=255)
```
---
#### Example 2: Calculating Disk Space (Total, Free, and Used)
You can use the attributes of the returned object to calculate human-readable storage metrics (such as Gigabytes) for a given directory.
```python
#!/usr/bin/python3
import os
def get_disk_usage(path):
st = os.statvfs(path)
# Block size (fundamental block size is used for size calculations)
block_size = st.f_frsize
# Calculate sizes in bytes
total_bytes = st.f_blocks * block_size
free_bytes = st.f_bfree * block_size
avail_bytes = st.f_bavail * block_size # Available to non-root users
used_bytes = total_bytes - free_bytes
# Convert bytes to Gigabytes (GB)
bytes_in_gb = 1024 ** 3
print(f"Disk Usage Info for '{path}':")
print(f" Total Space: {total_bytes / bytes_in_gb:.2f} GB")
print(f" Used Space: {used_bytes / bytes_in_gb:.2f} GB")
print(f" Available (User): {avail_bytes / bytes_in_gb:.2f} GB")
# Check disk usage of the root directory
get_disk_usage('/')
```
---
### Considerations
1. **Platform Availability**:
* This method is available on Unix-like operating systems (Linux, macOS, BSD, etc.).
* It is **not available on Windows**. For Windows systems, you can use `shutil.disk_usage(path)` which is cross-platform and provides a simpler interface for basic disk space queries.
2. **Block Size Calculations**:
* When calculating disk capacity, always multiply the block counts (`f_blocks`, `f_bfree`, `f_bavail`) by the fundamental block size (`f_frsize`) rather than the transfer block size (`f_bsize`), as they can occasionally differ depending on the underlying file system.
YouTip