|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | + |
| 6 | +parser = argparse.ArgumentParser(description="Take a directory path or a filename and calculate those sizes in KB") |
| 7 | +# Creating an ArgumentParser object |
| 8 | + |
| 9 | +parser.add_argument("-F", help="Choose file prefix for recursive search in directory") |
| 10 | +parser.add_argument("path", help="File or directory path for calculating size") |
| 11 | +# Adding arguments |
| 12 | + |
| 13 | +group = parser.add_mutually_exclusive_group() |
| 14 | +# Creating MutuallyExclusiveGroup object |
| 15 | + |
| 16 | +group.add_argument("-d", action="store_true", help="Directory name for calculate size in KB") |
| 17 | +group.add_argument("-f", action="store_true", help="File name for calculate file size in KB") |
| 18 | +# Adding mutually exclusive arguments [-d | -f] |
| 19 | + |
| 20 | +args = parser.parse_args() |
| 21 | +# Taking arguments from command line |
| 22 | + |
| 23 | +F_argument = args.F |
| 24 | +d_argument = args.d |
| 25 | +f_argument = args.f |
| 26 | +path = args.path |
| 27 | +# Unpacking arguments to variables |
| 28 | + |
| 29 | +is_dir = os.path.isdir(path) |
| 30 | +# Check if path is a directory not a file |
| 31 | + |
| 32 | +if F_argument and not d_argument and not f_argument: |
| 33 | + # If user uses [-F] option lonely |
| 34 | + print('[-F] option cannot be used alone') |
| 35 | + |
| 36 | +elif d_argument and is_dir and not f_argument and not F_argument: |
| 37 | + # If [-d] used and path is a directory |
| 38 | + |
| 39 | + def get_size(start_path): |
| 40 | + total_size = 0 |
| 41 | + for dirpath, dirnames, filenames in os.walk(start_path): |
| 42 | + for f in filenames: |
| 43 | + fp = os.path.join(dirpath, f) |
| 44 | + # skip if it is symbolic link |
| 45 | + if not os.path.islink(fp): |
| 46 | + total_size += os.path.getsize(fp) / 1024 |
| 47 | + # Calculate files sizes and convert to kb |
| 48 | + |
| 49 | + return total_size |
| 50 | + |
| 51 | + |
| 52 | + print(f"Size of files in directory: {get_size(path):.3f} KB") |
| 53 | + |
| 54 | +elif d_argument and not is_dir and not f_argument and not F_argument: |
| 55 | + # If user uses -d option with a file path not a directory |
| 56 | + print('Must use a directory path with [ -d ].') |
| 57 | + |
| 58 | +elif f_argument and not is_dir and not d_argument and not F_argument: |
| 59 | + # Id [-f] option used and a file name was entered |
| 60 | + file_size = os.path.getsize(path) / 1024 |
| 61 | + # Calculate file size and convert to kb |
| 62 | + print(f"Size of file {path} is: {file_size:.3f} KB") |
| 63 | + |
| 64 | +elif f_argument and is_dir and not d_argument and not F_argument: |
| 65 | + # If user uses [-f] option with a directory path not a file path |
| 66 | + print('Must use [ -f ] with a file name not a directory path') |
| 67 | + |
| 68 | +elif f_argument and F_argument: |
| 69 | + # If user uses [-F] option with [-F] option |
| 70 | + print('You can not use [-F] option with [-f] option') |
| 71 | + |
| 72 | +elif F_argument and d_argument and is_dir and not f_argument: |
| 73 | + # If [-F] for search files with their prefixes in a [-d] directory |
| 74 | + def get_size(start_path): |
| 75 | + total_size = 0 |
| 76 | + for dirpath, dirnames, filenames in os.walk(start_path): |
| 77 | + for f in filenames: |
| 78 | + if f.endswith(F_argument): |
| 79 | + fp = os.path.join(dirpath, f) |
| 80 | + # skip if it is symbolic link |
| 81 | + if not os.path.islink(fp): |
| 82 | + total_size += os.path.getsize(fp) / 1024 |
| 83 | + # Calculate files sizes and convert to kb |
| 84 | + |
| 85 | + return total_size |
| 86 | + |
| 87 | + |
| 88 | + print(f"Size of {F_argument} files in directory: {get_size(path):.3f} KB") |
| 89 | + |
| 90 | +elif F_argument and d_argument and not is_dir and not f_argument: |
| 91 | + # If user uses [-F] option and [-d] option and a file path except directory path |
| 92 | + print('Must use [ -d ] option with a directory path') |
0 commit comments