From 17507e1a8749cfc06c8cfae7e376cbc279af01d4 Mon Sep 17 00:00:00 2001 From: Saeedahmadi Date: Fri, 18 Jun 2021 04:21:17 +0430 Subject: [PATCH 1/2] Adding README.md --- Gettin File and Folder sizes/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Gettin File and Folder sizes/README.md diff --git a/Gettin File and Folder sizes/README.md b/Gettin File and Folder sizes/README.md new file mode 100644 index 00000000..1cc7e4e2 --- /dev/null +++ b/Gettin File and Folder sizes/README.md @@ -0,0 +1,9 @@ +# This is a script that getting file and folders sizes + +# Its use argparse module to write user-friendly command-line interfaces. + +# also use OS module to provides functions for interacting with the operating system. + +# How we can use it? + +### python3 GettingSizes.py --help From c2733cff06def29a473b377ae74b4e1884798f1c Mon Sep 17 00:00:00 2001 From: Saeedahmadi Date: Fri, 18 Jun 2021 04:22:03 +0430 Subject: [PATCH 2/2] Adding GettingSizes script --- Gettin File and Folder sizes/GettingSizez.py | 92 ++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 Gettin File and Folder sizes/GettingSizez.py diff --git a/Gettin File and Folder sizes/GettingSizez.py b/Gettin File and Folder sizes/GettingSizez.py new file mode 100755 index 00000000..b6e7e806 --- /dev/null +++ b/Gettin File and Folder sizes/GettingSizez.py @@ -0,0 +1,92 @@ +#!/usr/bin/python3 + +import argparse +import os + +parser = argparse.ArgumentParser(description="Take a directory path or a filename and calculate those sizes in KB") +# Creating an ArgumentParser object + +parser.add_argument("-F", help="Choose file prefix for recursive search in directory") +parser.add_argument("path", help="File or directory path for calculating size") +# Adding arguments + +group = parser.add_mutually_exclusive_group() +# Creating MutuallyExclusiveGroup object + +group.add_argument("-d", action="store_true", help="Directory name for calculate size in KB") +group.add_argument("-f", action="store_true", help="File name for calculate file size in KB") +# Adding mutually exclusive arguments [-d | -f] + +args = parser.parse_args() +# Taking arguments from command line + +F_argument = args.F +d_argument = args.d +f_argument = args.f +path = args.path +# Unpacking arguments to variables + +is_dir = os.path.isdir(path) +# Check if path is a directory not a file + +if F_argument and not d_argument and not f_argument: + # If user uses [-F] option lonely + print('[-F] option cannot be used alone') + +elif d_argument and is_dir and not f_argument and not F_argument: + # If [-d] used and path is a directory + + def get_size(start_path): + total_size = 0 + for dirpath, dirnames, filenames in os.walk(start_path): + for f in filenames: + fp = os.path.join(dirpath, f) + # skip if it is symbolic link + if not os.path.islink(fp): + total_size += os.path.getsize(fp) / 1024 + # Calculate files sizes and convert to kb + + return total_size + + + print(f"Size of files in directory: {get_size(path):.3f} KB") + +elif d_argument and not is_dir and not f_argument and not F_argument: + # If user uses -d option with a file path not a directory + print('Must use a directory path with [ -d ].') + +elif f_argument and not is_dir and not d_argument and not F_argument: + # Id [-f] option used and a file name was entered + file_size = os.path.getsize(path) / 1024 + # Calculate file size and convert to kb + print(f"Size of file {path} is: {file_size:.3f} KB") + +elif f_argument and is_dir and not d_argument and not F_argument: + # If user uses [-f] option with a directory path not a file path + print('Must use [ -f ] with a file name not a directory path') + +elif f_argument and F_argument: + # If user uses [-F] option with [-F] option + print('You can not use [-F] option with [-f] option') + +elif F_argument and d_argument and is_dir and not f_argument: + # If [-F] for search files with their prefixes in a [-d] directory + def get_size(start_path): + total_size = 0 + for dirpath, dirnames, filenames in os.walk(start_path): + for f in filenames: + if f.endswith(F_argument): + fp = os.path.join(dirpath, f) + # skip if it is symbolic link + if not os.path.islink(fp): + total_size += os.path.getsize(fp) / 1024 + # Calculate files sizes and convert to kb + + return total_size + + + print(f"Size of {F_argument} files in directory: {get_size(path):.3f} KB") + +elif F_argument and d_argument and not is_dir and not f_argument: + # If user uses [-F] option and [-d] option and a file path except directory path + print('Must use [ -d ] option with a directory path')