From 5f53628934916db4ad2c1c30e7fbbde5a65c13b2 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Fri, 1 Aug 2025 16:58:24 +0100 Subject: [PATCH 01/10] wip: done -1 flag --- implement-shell-tools/ls/ls.py | 40 ++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 implement-shell-tools/ls/ls.py diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 00000000..6c184984 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,40 @@ +import os +import argparse +import sys + +parser = argparse.ArgumentParser( + prog="ls", + description="list directory contents", +) + +parser.add_argument("-1",dest="one",action='store_true', help="list one file per line") +parser.add_argument("-a", action='store_true', help="Used to list all files, including hidden files, in the current directory") +parser.add_argument("path", nargs="?", default=".", help="The path to search") + +args = parser.parse_args() + +def arguments_proceeding(files): + if args.one: + files.sort() + for f in files: + print(f) + + if not args.a: + files = [f for f in files if not f.startswith(".")] + + +def path_proceeding(path_argument): + if os.path.isfile(path_argument): + print(path_argument) + elif os.path.isdir(path_argument): + files = os.listdir(path_argument) + arguments_proceeding(files) +# elif not path_argument: +# print("no") + +# if not args.path: +# print("no") +path_proceeding(args.path) + + + From 66d8f5eb32e4897622e9a6b49fb429212a432b83 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 3 Aug 2025 07:40:42 +0100 Subject: [PATCH 02/10] done ls --- implement-shell-tools/ls/ls.py | 79 ++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 6c184984..dd72132b 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -1,6 +1,5 @@ import os import argparse -import sys parser = argparse.ArgumentParser( prog="ls", @@ -14,26 +13,88 @@ args = parser.parse_args() def arguments_proceeding(files): + data_to_proceed = files + + if args.a: + data_to_proceed = [f for f in files] + data_to_proceed.sort() + data_to_proceed.insert(0, "..") + data_to_proceed.insert(0, ".") + else: + data_to_proceed = [f for f in files if not f.startswith('.')] + data_to_proceed.sort(key=str.lower) if args.one: - files.sort() - for f in files: + output = [f for f in data_to_proceed] + for f in output: print(f) + else: + print(" ".join(data_to_proceed)) + + + + + + + + + + + + + - if not args.a: - files = [f for f in files if not f.startswith(".")] + + + +# output = files + +# # if args.one: +# # files.sort() +# # for f in files: +# # print(f) + +# if args.a: +# output = [f for f in files] +# output.sort() +# output.insert(0, "..") +# output.insert(0, ".") + +# if args.one: +# output = [f for f in files] +# output.sort() +# for f in output: +# print(f) + + +# ------- + +# if args.a: +# output = files # include hidden files +# output.insert(0, "..") +# output.insert(0, ".") +# else: +# # exclude hidden files +# output = [f for f in files if not f.startswith('.')] + +# output.sort() # always sort after filtering + +# if args.one: +# for f in output: +# print(f) +# else: +# print(" ".join(output)) + def path_proceeding(path_argument): if os.path.isfile(path_argument): print(path_argument) elif os.path.isdir(path_argument): files = os.listdir(path_argument) arguments_proceeding(files) -# elif not path_argument: -# print("no") + + -# if not args.path: -# print("no") path_proceeding(args.path) From 573074cba58f5b4afc66c87ff4465aadceb4c9bc Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 3 Aug 2025 07:53:58 +0100 Subject: [PATCH 03/10] removed unnecessary comments and created wc.py file --- implement-shell-tools/ls/ls.py | 55 ---------------------------------- implement-shell-tools/wc/wc.py | 0 2 files changed, 55 deletions(-) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index dd72132b..9f82e3ed 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -31,61 +31,6 @@ def arguments_proceeding(files): print(" ".join(data_to_proceed)) - - - - - - - - - - - - - - - - - -# output = files - -# # if args.one: -# # files.sort() -# # for f in files: -# # print(f) - -# if args.a: -# output = [f for f in files] -# output.sort() -# output.insert(0, "..") -# output.insert(0, ".") - -# if args.one: -# output = [f for f in files] -# output.sort() -# for f in output: -# print(f) - - -# ------- - -# if args.a: -# output = files # include hidden files -# output.insert(0, "..") -# output.insert(0, ".") -# else: -# # exclude hidden files -# output = [f for f in files if not f.startswith('.')] - -# output.sort() # always sort after filtering - -# if args.one: -# for f in output: -# print(f) -# else: -# print(" ".join(output)) - def path_proceeding(path_argument): if os.path.isfile(path_argument): print(path_argument) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 00000000..e69de29b From ace3d8eeaa7ebe4c027c8ff0f47fa1a599181e86 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 3 Aug 2025 10:07:38 +0100 Subject: [PATCH 04/10] done wc --- implement-shell-tools/wc/wc.py | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index e69de29b..422ac02c 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,55 @@ +import os +import argparse +import sys + +parser = argparse.ArgumentParser( + prog="wc", + description="Counts words in a file that contain a particular character", +) + +parser.add_argument("-l",action='store_true', help="The number of lines in each input file is written to the standard output.") +parser.add_argument("-w", action='store_true', help="The number of words in each input file is written to the standard output.") +parser.add_argument("-c", action='store_true', help="The number of bytes in each input file is written to the standard output.") +parser.add_argument("path", nargs="+", help="The path to search") + +args = parser.parse_args() + +if (not args.w and not args.c and not args.l) : + args.w = args.c = args.l = True +total = [] +for path in args.path: + output = [] + if args.l or args.w: + with open(path) as file: + lines = file.readlines() + # lines count + if args.l: + num_lines = len(lines) + output.append(num_lines) + # word count + if args.w: + word_count = 0 + for line in lines: + lin = line.rstrip() + wds = lin.split() + word_count += len(wds) + + output.append(word_count) + + + if args.c: + file_size = os.path.getsize(path) + output.append(file_size) + + if len(args.path) > 1: + total.append(output.copy()) + output.append(path) + string_list = map(str, output) + print(" ".join(string_list)) +if len(args.path) > 1: + result = [sum(i) for i in zip(*total)] + string_result_list = map(str, result) + print(" ".join(string_result_list), " total") + + + \ No newline at end of file From 0ddafe8227e8ef99a444727faaf1d0dd482cce3e Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 3 Aug 2025 12:17:44 +0100 Subject: [PATCH 05/10] done cat --- implement-shell-tools/cat/cat.py | 33 ++++++++++++++++++++++++++++++++ implement-shell-tools/wc/wc.py | 1 - 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 00000000..3c0d7eaa --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,33 @@ +import os +import argparse + +parser = argparse.ArgumentParser( + prog="cat", + description="Concatenate and print files", +) + +parser.add_argument("-n",action='store_true', help="Number the output lines, starting at 1") +parser.add_argument("-b", action='store_true', help="Number the non-blank output lines, starting at 1") +parser.add_argument("path", nargs="+", help="The path to search") + +args = parser.parse_args() +for path in args.path: + with open(path) as file: + lines = file.readlines() + line_num = 1 + for line in lines: + lin = line.rstrip('\n') + + if args.b: + if lin =="": + print() + else: + print(line_num, lin) + line_num += 1 + elif args.n: + print(line_num, lin) + line_num += 1 + + else: + print(lin) + diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 422ac02c..d91d133b 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -1,6 +1,5 @@ import os import argparse -import sys parser = argparse.ArgumentParser( prog="wc", From 8544e5656cffe7d8ca7b0efe21deb567344699d6 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Wed, 20 Aug 2025 18:53:50 +0100 Subject: [PATCH 06/10] done changes according to review --- implement-shell-tools/cat/cat.py | 16 +++++++++------- implement-shell-tools/ls/ls.py | 7 +------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 3c0d7eaa..e3f73283 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -1,4 +1,3 @@ -import os import argparse parser = argparse.ArgumentParser( @@ -6,8 +5,12 @@ description="Concatenate and print files", ) -parser.add_argument("-n",action='store_true', help="Number the output lines, starting at 1") -parser.add_argument("-b", action='store_true', help="Number the non-blank output lines, starting at 1") +parser.add_argument( + "-n", action="store_true", help="Number the output lines, starting at 1" +) +parser.add_argument( + "-b", action="store_true", help="Number the non-blank output lines, starting at 1" +) parser.add_argument("path", nargs="+", help="The path to search") args = parser.parse_args() @@ -16,18 +19,17 @@ lines = file.readlines() line_num = 1 for line in lines: - lin = line.rstrip('\n') + lin = line.rstrip("\n") if args.b: - if lin =="": + if lin == "": print() else: print(line_num, lin) - line_num += 1 + line_num += 1 elif args.n: print(line_num, lin) line_num += 1 else: print(lin) - diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 9f82e3ed..c45ecd81 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -38,9 +38,4 @@ def path_proceeding(path_argument): files = os.listdir(path_argument) arguments_proceeding(files) - - -path_proceeding(args.path) - - - +path_proceeding(args.path) \ No newline at end of file From f9ea264be2bb1d584d72864a61eade7ff070eb6b Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 7 Sep 2025 13:41:47 +0100 Subject: [PATCH 07/10] fixed cat --- implement-shell-tools/cat/cat.py | 35 +++++++++++++++++--------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index e3f73283..7225c33a 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -1,4 +1,5 @@ import argparse +import os parser = argparse.ArgumentParser( prog="cat", @@ -15,21 +16,23 @@ args = parser.parse_args() for path in args.path: - with open(path) as file: - lines = file.readlines() - line_num = 1 - for line in lines: - lin = line.rstrip("\n") - - if args.b: - if lin == "": - print() - else: - print(line_num, lin) + if os.path.isfile(path): + with open(path) as file: + lines = file.readlines() + line_num = 1 + for line in lines: + lin = line.rstrip("\n") + if args.b: + if lin == "": + print() + else: + print(line_num, lin) + line_num += 1 + elif args.n: + print(" ", line_num, lin) line_num += 1 - elif args.n: - print(line_num, lin) - line_num += 1 - else: - print(lin) + else: + print(lin) + else: + print(f"cat: {path}: Is a directory") From 5ddd011f234d5a9b35e1bd614b4c50f4906fbf27 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 7 Sep 2025 18:37:43 +0100 Subject: [PATCH 08/10] fixed wc --- implement-shell-tools/wc/wc.py | 67 ++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index d91d133b..e6bad606 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -6,49 +6,78 @@ description="Counts words in a file that contain a particular character", ) -parser.add_argument("-l",action='store_true', help="The number of lines in each input file is written to the standard output.") -parser.add_argument("-w", action='store_true', help="The number of words in each input file is written to the standard output.") -parser.add_argument("-c", action='store_true', help="The number of bytes in each input file is written to the standard output.") +parser.add_argument( + "-l", + action="store_true", + help="The number of lines in each input file is written to the standard output.", +) +parser.add_argument( + "-w", + action="store_true", + help="The number of words in each input file is written to the standard output.", +) +parser.add_argument( + "-c", + action="store_true", + help="The number of bytes in each input file is written to the standard output.", +) parser.add_argument("path", nargs="+", help="The path to search") args = parser.parse_args() -if (not args.w and not args.c and not args.l) : +if not args.w and not args.c and not args.l: args.w = args.c = args.l = True -total = [] +numbers = [] +output = [] + for path in args.path: - output = [] + output_for_one_file = [] if args.l or args.w: with open(path) as file: lines = file.readlines() # lines count if args.l: num_lines = len(lines) - output.append(num_lines) + output_for_one_file.append(num_lines) # word count if args.w: word_count = 0 - for line in lines: + for line in lines: lin = line.rstrip() wds = lin.split() word_count += len(wds) - output.append(word_count) - + output_for_one_file.append(word_count) if args.c: file_size = os.path.getsize(path) - output.append(file_size) + output_for_one_file.append(file_size) if len(args.path) > 1: - total.append(output.copy()) - output.append(path) - string_list = map(str, output) - print(" ".join(string_list)) + numbers.append(output_for_one_file.copy()) + output_for_one_file.append(path) + output.append(output_for_one_file) if len(args.path) > 1: - result = [sum(i) for i in zip(*total)] - string_result_list = map(str, result) - print(" ".join(string_result_list), " total") + total_results = [sum(i) for i in zip(*numbers)] + total_results.append("total") + output.append(total_results) + num_cols = len(total_results) - 1 + column_widths = [len(str(total_results[i])) for i in range(num_cols)] - \ No newline at end of file + for row in output: + line_parts = [] + for i in range(num_cols): + # Right-align with 1 space padding (like wc) + line_parts.append(str(row[i]).rjust(column_widths[i] + 7)) + print("".join(line_parts), row[-1]) +elif len(args.path) == 1: + num_cols = len(output[0]) - 1 + print(num_cols) + column_widths = [len(str([output[0][i]])) for i in range(num_cols)] + for row in output: + line_parts = [] + for i in range(num_cols): + # Right-align with 1 space padding (like wc) + line_parts.append(str(row[i]).rjust(column_widths[i] + 6)) + print("".join(line_parts), row[-1]) From 350c3f28cdb748deff18c0a428036218b5b700f5 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 7 Sep 2025 18:40:07 +0100 Subject: [PATCH 09/10] done changes according to review --- implement-shell-tools/wc/wc.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index e6bad606..0aa45505 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -29,7 +29,7 @@ args.w = args.c = args.l = True numbers = [] output = [] - +column_widths = [] for path in args.path: output_for_one_file = [] if args.l or args.w: @@ -64,20 +64,12 @@ num_cols = len(total_results) - 1 column_widths = [len(str(total_results[i])) for i in range(num_cols)] - - for row in output: - line_parts = [] - for i in range(num_cols): - # Right-align with 1 space padding (like wc) - line_parts.append(str(row[i]).rjust(column_widths[i] + 7)) - print("".join(line_parts), row[-1]) elif len(args.path) == 1: num_cols = len(output[0]) - 1 - print(num_cols) column_widths = [len(str([output[0][i]])) for i in range(num_cols)] - for row in output: - line_parts = [] - for i in range(num_cols): - # Right-align with 1 space padding (like wc) - line_parts.append(str(row[i]).rjust(column_widths[i] + 6)) - print("".join(line_parts), row[-1]) +for row in output: + line_parts = [] + for i in range(num_cols): + # Right-align with 1 space padding (like wc) + line_parts.append(str(row[i]).rjust(column_widths[i] + 6)) + print("".join(line_parts), row[-1]) From f8941fb0613cb47205f6f6706c1dbfb41fd50e66 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 7 Sep 2025 18:50:16 +0100 Subject: [PATCH 10/10] added right-justify to output --- implement-shell-tools/cat/cat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 7225c33a..11dfc694 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -26,10 +26,10 @@ if lin == "": print() else: - print(line_num, lin) + print(str(line_num).rjust(6), lin) line_num += 1 elif args.n: - print(" ", line_num, lin) + print(str(line_num).rjust(6), "", lin) line_num += 1 else: