Created
May 27, 2025 05:32
-
-
Save RajChowdhury240/83801c70f3d90412974f55685312f07f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shutil | |
from rich.console import Console | |
from rich.table import Table | |
def get_disk_usage(path="/"): | |
total, used, free = shutil.disk_usage(path) | |
return { | |
"Mount Point": path, | |
"Total (GB)": f"{total / (1024 ** 3):.2f}", | |
"Used (GB)": f"{used / (1024 ** 3):.2f}", | |
"Free (GB)": f"{free / (1024 ** 3):.2f}", | |
"Used %": f"{(used / total) * 100:.2f}%", | |
"Free %": f"{(free / total) * 100:.2f}%" | |
} | |
def display_disk_table(paths=["/"]): | |
console = Console() | |
table = Table(title="Disk Usage Info", show_lines=True) | |
table.add_column("Mount Point", style="cyan", no_wrap=True) | |
table.add_column("Total (GB)", justify="right") | |
table.add_column("Used (GB)", justify="right") | |
table.add_column("Free (GB)", justify="right") | |
table.add_column("Used %", justify="right", style="yellow") | |
table.add_column("Free %", justify="right", style="green") | |
for path in paths: | |
data = get_disk_usage(path) | |
table.add_row( | |
data["Mount Point"], | |
data["Total (GB)"], | |
data["Used (GB)"], | |
data["Free (GB)"], | |
data["Used %"], | |
data["Free %"] | |
) | |
console.print(table) | |
if __name__ == "__main__": | |
display_disk_table(paths=["/"]) | |
# display_disk_table(paths=["/Users/raj"]) , can add as many mount path to check as per your need |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment