Advertisement
Python253

is_mounted

May 10th, 2024
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: is_mounted.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script enables users to retrieve essential data about each mounted filesystem.
  9.  
  10. Information Included:
  11.    - Device: The device associated with the mounted filesystem (e.g., C:\, D:\ on Windows, or /dev/sda1, /dev/sdb1 on Unix-like systems).
  12.    - Filesystem Type: The type of filesystem used on the device (e.g., NTFS, ext4, FAT32).
  13.    - Total Size: The total size of the filesystem, indicating the overall storage capacity available.
  14.    - Used Space: The amount of space currently utilized within the filesystem.
  15.    - Available Space: The remaining available space within the filesystem that can be utilized.
  16.    - Usage Percentage: The percentage of space currently utilized in relation to the total capacity.
  17.  
  18. Functions:
  19.    1. main(): Main function to fetch and display mounted filesystems information.
  20.    2. bytes_to_human_readable(bytes): Converts bytes to a human-readable format.
  21.  
  22. Requirements:
  23.    - Python 3.x
  24.    - psutil Module
  25.  
  26. Usage:
  27.    - Run the script to display information about mounted filesystems.
  28.  
  29. Expected Output:
  30. --------------------------------------------------------
  31.    Fetching Mounted Filesystems...
  32.  
  33.    Mounted Filesystems Found:
  34.  
  35.        :: [Mount Point: C:\] ::
  36.  
  37.          - Device: C:\
  38.          - Filesystem Type: NTFS
  39.          - Total Size: 474.73 GB
  40.          - Used Space: 124.94 GB
  41.          - Available Space: 349.79 GB
  42.          - Usage Percentage: 26.3%
  43.  
  44.        :: [Mount Point: D:\] ::
  45.  
  46.          - Device: D:\
  47.          - Filesystem Type: NTFS
  48.          - Total Size: 931.50 GB
  49.          - Used Space: 165.80 GB
  50.          - Available Space: 765.69 GB
  51.          - Usage Percentage: 17.8%
  52. --------------------------------------------------------
  53.          
  54. Additional Notes:
  55.    - This script requires the psutil module to be installed.
  56.    - You can install it using pip command:
  57.        
  58.            'pip install psutil'
  59.            
  60.    - Ensure that the script has the necessary permissions to access mounted filesystem information.
  61.    - By executing the script, users gain insights into the storage utilization of each mounted filesystem,
  62.      empowering them to make informed decisions regarding resource allocation, storage management & system performance optimization.
  63. """
  64.  
  65. import psutil
  66.  
  67. def main():
  68.     """
  69.    Main function to fetch and display mounted filesystems information.
  70.    """
  71.     print("Fetching Mounted Filesystems...\n")
  72.     try:
  73.         mounts = psutil.disk_partitions()
  74.         if not mounts:
  75.             print("No filesystems are mounted.")
  76.         else:
  77.             print("Mounted Filesystems Found:\n")
  78.             for mount in mounts:
  79.                 print(f"\t:: [Mount Point: {mount.mountpoint}] ::\n")
  80.                 usage = psutil.disk_usage(mount.mountpoint)
  81.                 print(f"\t  - Device: {mount.device}")
  82.                 print(f"\t  - Filesystem Type: {mount.fstype}")
  83.                 print(f"\t  - Total Size: {bytes_to_human_readable(usage.total)}")
  84.                 print(f"\t  - Used Space: {bytes_to_human_readable(usage.used)}")
  85.                 print(f"\t  - Available Space: {bytes_to_human_readable(usage.free)}")
  86.                 print(f"\t  - Usage Percentage: {usage.percent}%")
  87.                 print()
  88.     except Exception as e:
  89.         print(f"\n\tError: {e}")
  90.  
  91. def bytes_to_human_readable(bytes):
  92.     """
  93.    Convert bytes to a human-readable format (e.g., KB, MB, GB).
  94.    """
  95.     for unit in ["", "KB", "MB", "GB", "TB"]:
  96.         if bytes < 1024:
  97.             return f"{bytes:.2f} {unit}"
  98.         bytes /= 1024
  99.  
  100. if __name__ == "__main__":
  101.     main()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement