From 3383e76ac5286c4ae9dca23b9a91334f912953d6 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Mon, 1 Dec 2025 13:32:47 +0000 Subject: [PATCH 1/9] My Cat in Python --- implement-shell-tools/cat/cat.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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 000000000..49cd1e395 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,30 @@ +import argparse +parser = argparse.ArgumentParser( + prog="count-containing-words", + description="Counts words in a file that contain a particular character", +) + +counterNumber = 1 +parser.add_argument("-n", action="store_true", help="number all lines") +parser.add_argument("-b", action="store_true", help="The character to search for" ) +parser.add_argument("path", nargs="+", help="The file to search") + +args = parser.parse_args() + +for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + + if args.b: + for i in range(len(arrText )): + if arrText[i].strip() != "": + print(counterNumber,arrText[i]) + counterNumber += 1 + + elif args.n: + for i in range(len(arrText )): + print(counterNumber, arrText[i]) + counterNumber += 1 + else: + print(content) \ No newline at end of file From 7ba19f8079b1b0f20ebfcf6842903cc8b39369bb Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Mon, 1 Dec 2025 13:34:47 +0000 Subject: [PATCH 2/9] change Cat description and create ls.py file --- implement-shell-tools/cat/cat.py | 4 ++-- implement-shell-tools/ls/ls.py | 0 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 implement-shell-tools/ls/ls.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 49cd1e395..98b8b753b 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -1,7 +1,7 @@ import argparse parser = argparse.ArgumentParser( - prog="count-containing-words", - description="Counts words in a file that contain a particular character", + prog="my-cat", + description="Simple cat clone with -n and -b options", ) counterNumber = 1 diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..e69de29bb From 419f1dbae86d69f227736b0e94f090eb668512ea Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Mon, 1 Dec 2025 14:27:01 +0000 Subject: [PATCH 3/9] MyLs in python --- implement-shell-tools/ls/ls.py | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index e69de29bb..b5a9c9ab1 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,39 @@ +import argparse +import os + +def show_unhidden_files(listDir): + for file in listDir: + if not file.startswith('.'): + print(file) +def show_all_files(listDir): + for file in listDir: + print(file) + + +parser = argparse.ArgumentParser( + prog="my-ls", + description="Simple ls clone with -a and -l options", +) + +parser.add_argument("-a", action="store_true", help="include hidden files") +parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format") +parser.add_argument("path", nargs="?", default=".", help="The directory to list") +args = parser.parse_args() + + + + + +fn = args.path +listDir = os.listdir(fn) +if fn!="": + if args.a and args.one: + show_all_files(listDir) + elif args.a: + show_all_files(listDir) + elif args.one: + show_unhidden_files(listDir) + + else: + show_unhidden_files(listDir) + From 37dc584f13cb624d80d35438074cee6868154e60 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Mon, 1 Dec 2025 17:02:02 +0000 Subject: [PATCH 4/9] MyWc in Python --- implement-shell-tools/wc/wc.py | 66 ++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..8e139ddc8 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,66 @@ +import argparse +parser = argparse.ArgumentParser( + prog="my-wc", + description="Simple wc clone with -l and -w options", +) +lineCounter = 0 +wordCounter = 0 +charCounter = 0 +parser.add_argument("-l", action="store_true", help="count lines") +parser.add_argument("-w", action="store_true", help="count words") +parser.add_argument("-c", action="store_true", help="count characters") +parser.add_argument("path", nargs="+", default=".", help="The file to count") +args = parser.parse_args() + +if not args.l and not args.w and not args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + arrWords = content.split() + wordCounter += len(arrWords) + charCounter += len(content) + print("Line count:", lineCounter,"lines") + print("Word count:", wordCounter,"words") + print("Character count:", charCounter,"characters") +elif args.l and args.w: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + arrWords = content.split() + wordCounter += len(arrWords) + print("Line count:", lineCounter,"lines") + print("Word count:", wordCounter,"words") +elif args.l and args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + charCounter += len(content) + print("Line count:", lineCounter,"lines") + print("Character count:", charCounter,"characters") +elif args.l: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split("\n") + lineCounter += len(arrText) + print("Line count:", lineCounter,"lines") + +elif args.w: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + arrText = content.split() + wordCounter += len(arrText) + print("Word count:", wordCounter,"words") +elif args.c: + for file_path in args.path: + with open(file_path, "r") as f: + content = f.read() + charCounter += len(content) + print("Character count:", charCounter,"characters") \ No newline at end of file From ae5997751a837727626182c6cb224f8d80ed761b Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Wed, 11 Feb 2026 00:00:21 +0000 Subject: [PATCH 5/9] Update files: cat/ls/wc --- implement-shell-tools/cat/cat.py | 56 +++++++++++-------- implement-shell-tools/ls/ls.py | 32 +++++------ implement-shell-tools/wc/wc.py | 93 ++++++++++++++------------------ 3 files changed, 88 insertions(+), 93 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 98b8b753b..2befbac49 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -1,30 +1,44 @@ import argparse -parser = argparse.ArgumentParser( - prog="my-cat", - description="Simple cat clone with -n and -b options", -) -counterNumber = 1 -parser.add_argument("-n", action="store_true", help="number all lines") -parser.add_argument("-b", action="store_true", help="The character to search for" ) -parser.add_argument("path", nargs="+", help="The file to search") -args = parser.parse_args() +def run(args): + for file_path in args.path: + counter_number = 1 -for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split("\n") + with open(file_path, "r") as f: + lines = f.readlines() if args.b: - for i in range(len(arrText )): - if arrText[i].strip() != "": - print(counterNumber,arrText[i]) - counterNumber += 1 + for line in lines: + if line.strip() != "": + print(counter_number, line, end="") + counter_number += 1 + else: + print(line, end="") elif args.n: - for i in range(len(arrText )): - print(counterNumber, arrText[i]) - counterNumber += 1 + for line in lines: + print(counter_number, line, end="") + counter_number += 1 + else: - print(content) \ No newline at end of file + for line in lines: + print(line, end="") + + +def main(): + parser = argparse.ArgumentParser( + prog="my-cat", + description="Simple cat clone with -n and -b options", + ) + + parser.add_argument("-n", action="store_true", help="number all lines") + parser.add_argument("-b", action="store_true", help="The character to search for") + parser.add_argument("path", nargs="+", help="The file to search") + + args = parser.parse_args() + run(args) + + +if __name__ == "__main__": + main() diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index b5a9c9ab1..8a9fb8a76 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -1,13 +1,14 @@ import argparse import os -def show_unhidden_files(listDir): - for file in listDir: - if not file.startswith('.'): + +def show_files(files, show_hidden): + for file in files: + if show_hidden: print(file) -def show_all_files(listDir): - for file in listDir: - print(file) + else: + if not file.startswith("."): + print(file) parser = argparse.ArgumentParser( @@ -16,24 +17,15 @@ def show_all_files(listDir): ) parser.add_argument("-a", action="store_true", help="include hidden files") -parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format") +parser.add_argument( + "-1", dest="one", action="store_true", help="list one entry per line" +) parser.add_argument("path", nargs="?", default=".", help="The directory to list") args = parser.parse_args() - - - fn = args.path listDir = os.listdir(fn) -if fn!="": - if args.a and args.one: - show_all_files(listDir) - elif args.a: - show_all_files(listDir) - elif args.one: - show_unhidden_files(listDir) - - else: - show_unhidden_files(listDir) +if fn != "": + show_files(listDir, args.a) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 8e139ddc8..b620bc291 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -1,66 +1,55 @@ import argparse + parser = argparse.ArgumentParser( prog="my-wc", description="Simple wc clone with -l and -w options", ) + + + + +def read_file(file_path): + with open(file_path, "r") as file: + return file.read() + + +def count_text(text): + line_count = text.count("\n") + word_count = len(text.split()) + char_count = len(text) + return line_count, word_count, char_count + + + + lineCounter = 0 wordCounter = 0 charCounter = 0 + parser.add_argument("-l", action="store_true", help="count lines") parser.add_argument("-w", action="store_true", help="count words") parser.add_argument("-c", action="store_true", help="count characters") parser.add_argument("path", nargs="+", default=".", help="The file to count") args = parser.parse_args() -if not args.l and not args.w and not args.c: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split("\n") - lineCounter += len(arrText) - arrWords = content.split() - wordCounter += len(arrWords) - charCounter += len(content) - print("Line count:", lineCounter,"lines") - print("Word count:", wordCounter,"words") - print("Character count:", charCounter,"characters") -elif args.l and args.w: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split("\n") - lineCounter += len(arrText) - arrWords = content.split() - wordCounter += len(arrWords) - print("Line count:", lineCounter,"lines") - print("Word count:", wordCounter,"words") -elif args.l and args.c: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split("\n") - lineCounter += len(arrText) - charCounter += len(content) - print("Line count:", lineCounter,"lines") - print("Character count:", charCounter,"characters") -elif args.l: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split("\n") - lineCounter += len(arrText) - print("Line count:", lineCounter,"lines") - -elif args.w: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - arrText = content.split() - wordCounter += len(arrText) - print("Word count:", wordCounter,"words") -elif args.c: - for file_path in args.path: - with open(file_path, "r") as f: - content = f.read() - charCounter += len(content) - print("Character count:", charCounter,"characters") \ No newline at end of file + + +for path in args.path: + text = read_file(path) + lines, words, chars = count_text(text) + + lineCounter += lines + wordCounter += words + charCounter += chars + + if args.l: + print(lines, path) + elif args.w: + print(words, path) + elif args.c: + print(chars, path) + else: + print(lines, words, chars, path) + +if len(args.path) > 1 and not args.l and not args.w and not args.c: + print(lineCounter, wordCounter, charCounter, "total") \ No newline at end of file From 01967a3fdfbcb10e0d11e00a7ef5adf5b4f7eba4 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Fri, 20 Feb 2026 21:45:02 +0000 Subject: [PATCH 6/9] Refactor line numbering logic --- .gitignore | 5 +++++ implement-shell-tools/cat/.gitignore | 4 ++++ implement-shell-tools/cat/cat.py | 20 +++++--------------- 3 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 implement-shell-tools/cat/.gitignore diff --git a/.gitignore b/.gitignore index 3c3629e64..8f9f6b3a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ node_modules +.DS_Store +__pycache__/ +*.pyc +venv/ +type/ diff --git a/implement-shell-tools/cat/.gitignore b/implement-shell-tools/cat/.gitignore new file mode 100644 index 000000000..d6357197e --- /dev/null +++ b/implement-shell-tools/cat/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +__pycache__/ +*.pyc +venv/ diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index 2befbac49..ce4c5ecea 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -7,24 +7,14 @@ def run(args): with open(file_path, "r") as f: lines = f.readlines() - - if args.b: for line in lines: - if line.strip() != "": - print(counter_number, line, end="") + prefix = "" + if args.n or (args.b and line.strip() != ""): + prefix = counter_number counter_number += 1 - else: - print(line, end="") - - elif args.n: - for line in lines: - print(counter_number, line, end="") - counter_number += 1 - - else: - for line in lines: - print(line, end="") + print(f"{prefix} {line}", end="") + def main(): parser = argparse.ArgumentParser( From cc5dbf95fc6ab1f54ab8b66802611ec522b6f2e2 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Fri, 20 Feb 2026 22:12:57 +0000 Subject: [PATCH 7/9] refactor line and Var --- implement-shell-tools/ls/ls.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 8a9fb8a76..20e0c5862 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -4,11 +4,8 @@ def show_files(files, show_hidden): for file in files: - if show_hidden: - print(file) - else: - if not file.startswith("."): - print(file) + if show_hidden or not file.startswith("."): + print(file) parser = argparse.ArgumentParser( @@ -24,8 +21,6 @@ def show_files(files, show_hidden): args = parser.parse_args() -fn = args.path -listDir = os.listdir(fn) +directory_path = args.path +listDir = os.listdir(directory_path) -if fn != "": - show_files(listDir, args.a) From f73102ebbdfbd3040e64edbb4fb9ec352982d72a Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Sat, 21 Feb 2026 00:50:59 +0000 Subject: [PATCH 8/9] Add total with flags --- implement-shell-tools/wc/wc.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index b620bc291..0e34cabbe 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -51,5 +51,12 @@ def count_text(text): else: print(lines, words, chars, path) -if len(args.path) > 1 and not args.l and not args.w and not args.c: - print(lineCounter, wordCounter, charCounter, "total") \ No newline at end of file +if len(args.path) > 1: + if args.l: + print(lineCounter, "total") + elif args.w: + print(wordCounter, "total") + elif args.c: + print(charCounter, "total") + else: + print(lineCounter, wordCounter, charCounter, "total") \ No newline at end of file From ef5c3429b6215b3c2316506e5959bc09c3e2b0b7 Mon Sep 17 00:00:00 2001 From: Aida Eslami Date: Sat, 21 Feb 2026 00:58:38 +0000 Subject: [PATCH 9/9] formatting with ruff --- implement-shell-tools/cat/cat.py | 1 - implement-shell-tools/ls/ls.py | 3 +-- implement-shell-tools/wc/wc.py | 7 +------ 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index ce4c5ecea..3077ec3e4 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -14,7 +14,6 @@ def run(args): counter_number += 1 print(f"{prefix} {line}", end="") - def main(): parser = argparse.ArgumentParser( diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 20e0c5862..aae3ff508 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -5,7 +5,7 @@ def show_files(files, show_hidden): for file in files: if show_hidden or not file.startswith("."): - print(file) + print(file) parser = argparse.ArgumentParser( @@ -23,4 +23,3 @@ def show_files(files, show_hidden): directory_path = args.path listDir = os.listdir(directory_path) - diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index 0e34cabbe..aa8a2e86f 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -6,8 +6,6 @@ ) - - def read_file(file_path): with open(file_path, "r") as file: return file.read() @@ -20,8 +18,6 @@ def count_text(text): return line_count, word_count, char_count - - lineCounter = 0 wordCounter = 0 charCounter = 0 @@ -33,7 +29,6 @@ def count_text(text): args = parser.parse_args() - for path in args.path: text = read_file(path) lines, words, chars = count_text(text) @@ -59,4 +54,4 @@ def count_text(text): elif args.c: print(charCounter, "total") else: - print(lineCounter, wordCounter, charCounter, "total") \ No newline at end of file + print(lineCounter, wordCounter, charCounter, "total")