From b3124371b712d832ba011e0624be3f08df9f69ba Mon Sep 17 00:00:00 2001 From: Kalpita Birhade <84992682+kalpitabirhade28@users.noreply.github.com> Date: Thu, 19 Dec 2024 17:40:12 +0530 Subject: [PATCH] Create fF Improvements: Simplified output: Displays folder size in the most appropriate human-readable unit. Error handling: Checks if the provided directory exists. Readable structure: Uses helper functions for size calculation and formatting. Usability: Prints clear usage instructions when arguments are missing. --- fF | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 fF diff --git a/fF b/fF new file mode 100644 index 00000000000..2edac5d9f5d --- /dev/null +++ b/fF @@ -0,0 +1,43 @@ +# Script Name : folder_size.py +# Author : Craig Richards (Simplified by Assistant) +# Created : 19th July 2012 +# Last Modified : 19th December 2024 +# Version : 2.0.0 + +# Description : Scans a directory and subdirectories to display the total size. + +import os +import sys + +def get_folder_size(directory): + """Calculate the total size of a directory and its subdirectories.""" + total_size = 0 + for root, _, files in os.walk(directory): + for file in files: + total_size += os.path.getsize(os.path.join(root, file)) + return total_size + +def format_size(size): + """Format the size into human-readable units.""" + units = ["Bytes", "KB", "MB", "GB", "TB"] + for unit in units: + if size < 1024 or unit == units[-1]: + return f"{size:.2f} {unit}" + size /= 1024 + +def main(): + if len(sys.argv) < 2: + print("Usage: python folder_size.py ") + sys.exit(1) + + directory = sys.argv[1] + + if not os.path.exists(directory): + print(f"Error: The directory '{directory}' does not exist.") + sys.exit(1) + + folder_size = get_folder_size(directory) + print(f"Folder Size: {format_size(folder_size)}") + +if __name__ == "__main__": + main()