YouTip LogoYouTip

Python3 Os Fstatvfs

# Python3 os.fstatvfs() Method The `os.fstatvfs()` method in Python is used to retrieve system information for the filesystem containing the file associated with the file descriptor `fd`. This method is highly useful for system-level programming when you need to query disk space, block sizes, or filesystem limits using an open file descriptor. --- ## Introduction The `os.fstatvfs()` method returns information about the filesystem containing the file associated with the file descriptor `fd`. It is equivalent to `os.statvfs(fd)` in Python 3.3 and later. > **Availability:** Unix-like systems (Linux, macOS, etc.). It is not available on Windows. ### The `statvfs` Structure The method returns an object whose attributes describe the filesystem. The structure contains the following fields: | Attribute | Description | | :--- | :--- | | `f_bsize` | Filesystem block size | | `f_frsize` | Fragment size | | `f_blocks` | Total number of blocks in the filesystem | | `f_bfree` | 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` | Number of free file nodes | | `f_favail` | Number of free file nodes available to non-privileged users | | `f_fsid` | Filesystem ID | | `f_flag` | Mount flags (system-dependent, e.g., read-only, no-exec) | | `f_namemax` | Maximum filename length | --- ## Syntax ```python os.fstatvfs(fd) ``` ### Parameters * **`fd`**: The file descriptor of an open file. ### Return Value This method returns a `statvfs_result` object containing the filesystem attributes listed above. --- ## Code Example The following example demonstrates how to open a file, retrieve its filesystem information using `os.fstatvfs()`, extract specific attributes, and safely close the file descriptor. ```python #!/usr/bin/python3 import os # Open a file to get its file descriptor fd = os.open("test_file.txt", os.O_RDWR | os.O_CREAT) try: # Retrieve filesystem information using the file descriptor info = os.fstatvfs(fd) print("Filesystem Info Object:", info) # Extract specific attributes print(f"Maximum filename length: {info.f_namemax} characters") print(f"Total blocks: {info.f_blocks}") print(f"Free blocks available: {info.f_bfree}") print(f"Block size: {info.f_bsize} bytes") # Calculate total and free space in Gigabytes (GB) total_space_gb = (info.f_blocks * info.f_frsize) / (1024 ** 3) free_space_gb = (info.f_bfree * info.f_frsize) / (1024 ** 3) print(f"Total Filesystem Capacity: {total_space_gb:.2f} GB") print(f"Free Filesystem Space: {free_space_gb:.2f} GB") finally: # Always ensure the file descriptor is closed os.close(fd) ``` ### Example Output When executed on a Unix-like system, the output will look similar to this: ```text Filesystem Info Object: os.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=61022208, f_bfree=38415211, f_bavail=35298412, f_files=15523840, f_ffree=14920112, f_favail=14920112, f_fsid=10454321, f_flag=4096, f_namemax=255) Maximum filename length: 255 characters Total blocks: 61022208 Free blocks available: 38415211 Block size: 4096 bytes Total Filesystem Capacity: 232.78 GB Free Filesystem Space: 146.54 GB ``` --- ## Considerations 1. **Platform Dependency**: This function is only available on Unix/Linux/macOS platforms. Attempting to run it on Windows will raise an `AttributeError: module 'os' has no attribute 'fstatvfs'`. 2. **File Descriptor Management**: Always ensure that file descriptors are closed properly using a `try...finally` block or by using high-level context managers to prevent resource leaks. 3. **`f_bsize` vs `f_frsize`**: When calculating actual disk space in bytes, you should multiply the number of blocks by the fragment size (`f_frsize`) rather than the block size (`f_bsize`), as some filesystems allocate space based on fragment sizes.
← Python3 Os FtruncatePython3 Os Fpathconf β†’