Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get a file system information using Python?
File system information includes attributes and metadata associated with files or directories, such as size, available space, and usage statistics. Python provides several modules like os, shutil, and third-party libraries like psutil to retrieve this information programmatically.
Using os.statvfs() for File System Statistics
The os.statvfs() function returns detailed file system information as a statvfs_result object with various attributes ?
import os
# Use current directory for demonstration
path = '.'
# Retrieve file system information
fs_info = os.statvfs(path)
# Print key file system attributes
print("File System Information:")
print("Available File Nodes:", fs_info.f_favail)
print("Total File Nodes:", fs_info.f_files)
print("Optimal Block Size:", fs_info.f_bsize)
# Calculate total and available space
total_space = fs_info.f_frsize * fs_info.f_blocks
available_space = fs_info.f_frsize * fs_info.f_bavail
print("Total Space (bytes):", total_space)
print("Available Space (bytes):", available_space)
File System Information: Available File Nodes: 6859438 Total File Nodes: 7208960 Optimal Block Size: 4096 Total Space (bytes): 115658190848 Available Space (bytes): 89632002048
Using shutil.disk_usage() for Disk Statistics
The shutil.disk_usage() function provides a simpler approach to get disk usage statistics ?
import shutil
# Use current directory
path = '.'
# Get disk usage information
usage = shutil.disk_usage(path)
# Print disk usage information
print("Disk Usage Information:")
print("Total Space:", usage.total)
print("Used Space:", usage.used)
print("Free Space:", usage.free)
# Convert to GB for readability
print("\nIn Gigabytes:")
print("Total Space: {:.2f} GB".format(usage.total / (1024**3)))
print("Used Space: {:.2f} GB".format(usage.used / (1024**3)))
print("Free Space: {:.2f} GB".format(usage.free / (1024**3)))
Disk Usage Information: Total Space: 115658190848 Used Space: 26008670208 Free Space: 89632743424 In Gigabytes: Total Space: 107.74 GB Used Space: 24.22 GB Free Space: 83.52 GB
Using psutil Library
The psutil library provides cross-platform system information access. Note that you need to install it first with pip install psutil ?
import psutil
# Get disk usage information
path = '.'
usage = psutil.disk_usage(path)
# Print disk usage information
print("Disk Usage Information:")
print("Total Space:", usage.total)
print("Used Space:", usage.used)
print("Free Space:", usage.free)
# Get additional disk partition information
partitions = psutil.disk_partitions()
for partition in partitions:
print(f"\nPartition: {partition.device}")
print(f"File System: {partition.fstype}")
print(f"Mount Point: {partition.mountpoint}")
Key Attributes of os.statvfs_result
| Attribute | Description |
|---|---|
f_bsize |
Optimal file system block size |
f_frsize |
Fundamental file system block size |
f_blocks |
Total number of blocks in file system |
f_bfree |
Number of free blocks |
f_bavail |
Number of available blocks for unprivileged users |
f_files |
Total number of file nodes |
f_favail |
Number of available file nodes |
Comparison of Methods
| Method | Pros | Cons |
|---|---|---|
os.statvfs() |
Built-in, detailed information | Unix/Linux only, complex attributes |
shutil.disk_usage() |
Simple, cross-platform | Limited information |
psutil |
Rich features, cross-platform | External dependency |
Conclusion
Use shutil.disk_usage() for simple cross-platform disk usage statistics. Use os.statvfs() for detailed Unix/Linux file system information. Use psutil for advanced cross-platform system monitoring capabilities.
