From 5f53628934916db4ad2c1c30e7fbbde5a65c13b2 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Fri, 1 Aug 2025 16:58:24 +0100 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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