From 77bcbde3e942cac13b72b6cc8f5f0196e192b0cc Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 2 Aug 2026 11:16:19 +0100 Subject: [PATCH 1/4] implementing shell command tools in python --- implement-shell-tools/wc/wc.py | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 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..cd7801c28 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,45 @@ +import argparse +parser = argparse.ArgumentParser(prog="wc") +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 bytes") +parser.add_argument("files", nargs="+") + +args = parser.parse_args() +total_lines = 0 +total_words = 0 +total_chars = 0 + +for file in args.files: + with open(file, "r") as f: + text = f.read() + + lines = text.count("\n") + words = len(text.split()) + chars = len(text) + + total_lines += lines + total_words += words + total_chars += chars + + if not args.l and not args.w and not args.c: + print(lines, words, chars, file) + else: + if args.l: + print(lines, end=" ") + if args.w: + print(words, end=" ") + if args.c: + print(chars, end=" ") + print(file) +if len(args.files) > 1: + if not args.l and not args.w and not args.c: + print(total_lines, total_words, total_chars, "total") + else: + if args.l: + print(total_lines, end=" ") + if args.w: + print(total_words, end=" ") + if args.c: + print(total_chars, end=" ") + print("total") \ No newline at end of file From 53b2c347b3525f15ee25a37c71506dbe78c9f553 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Sun, 2 Aug 2026 12:34:01 +0100 Subject: [PATCH 2/4] implementing shell command tools in python --- implement-shell-tools/cat/cat.py | 26 ++++++++++++++++++++++ implement-shell-tools/ls/ls.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py 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 new file mode 100644 index 000000000..e01d74c7d --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,26 @@ +import argparse +import sys + +parser = argparse.ArgumentParser(prog="cat") +parser.add_argument("-n", action="store_true") +parser.add_argument("-b", action="store_true") +parser.add_argument("files", nargs="+") + +args = parser.parse_args() + +line_number = 1 + +for file in args.files: + with open(file) as f: + for line in f: + if args.b: + if line.strip(): + print(f"{line_number:6}\t{line}", end="") + line_number += 1 + else: + print(line, end="") + elif args.n: + print(f"{line_number:6}\t{line}", end="") + line_number += 1 + else: + print(line, end="") \ No newline at end of file diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..73dfda96b --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,38 @@ +import argparse +import os + +parser = argparse.ArgumentParser(prog="ls") +parser.add_argument("-1", dest="one", action="store_true") +parser.add_argument("-a", action="store_true") +parser.add_argument("paths", nargs="*", default=["."]) + +args = parser.parse_args() + +# Print all files first +for path in args.paths: + if not os.path.isdir(path): + if args.one: + print(path) + else: + print(path, end=" ") +print() + +# Print directories +for path in args.paths: + if os.path.isdir(path): + + if len(args.paths) > 1: + print(f"\n{path}:") + + files = sorted(os.listdir(path)) + + if args.a: + files = [".", ".."] + files + else: + files = [f for f in files if not f.startswith(".")] + + if args.one: + for file in files: + print(file) + else: + print(" ".join(files)) \ No newline at end of file From da4ebd195b990b01d363fad31ba3a9eb289e7d55 Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 7 Aug 2026 21:01:07 +0100 Subject: [PATCH 3/4] refactoring ls.py file --- implement-shell-tools/cat/cat.py | 5 +++-- implement-shell-tools/ls/ls.py | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py index e01d74c7d..7af2320c2 100644 --- a/implement-shell-tools/cat/cat.py +++ b/implement-shell-tools/cat/cat.py @@ -8,9 +8,10 @@ args = parser.parse_args() -line_number = 1 + for file in args.files: + line_number = 1 with open(file) as f: for line in f: if args.b: @@ -20,7 +21,7 @@ else: print(line, end="") elif args.n: - print(f"{line_number:6}\t{line}", end="") + print(f"{line_number:6} {line}", end="") line_number += 1 else: print(line, end="") \ No newline at end of file diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py index 73dfda96b..42b7afb7b 100644 --- a/implement-shell-tools/ls/ls.py +++ b/implement-shell-tools/ls/ls.py @@ -14,7 +14,7 @@ if args.one: print(path) else: - print(path, end=" ") + print(path, end=" ") print() # Print directories @@ -35,4 +35,4 @@ for file in files: print(file) else: - print(" ".join(files)) \ No newline at end of file + print(" ".join(files)) \ No newline at end of file From c881883af5e776c1d4123d8f3990c0f7d13978bb Mon Sep 17 00:00:00 2001 From: shreefAhmedM Date: Fri, 7 Aug 2026 21:14:40 +0100 Subject: [PATCH 4/4] implementing shell tools command in python --- implement-shell-tools/wc/wc.py | 37 +++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index cd7801c28..6488932df 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -1,4 +1,5 @@ import argparse + parser = argparse.ArgumentParser(prog="wc") parser.add_argument("-l", action="store_true", help="Count lines") parser.add_argument("-w", action="store_true", help="Count words") @@ -6,6 +7,7 @@ parser.add_argument("files", nargs="+") args = parser.parse_args() + total_lines = 0 total_words = 0 total_chars = 0 @@ -15,31 +17,42 @@ text = f.read() lines = text.count("\n") - words = len(text.split()) - chars = len(text) + + stripped = text.strip() + words = len(stripped.split()) if stripped else 0 + + chars = len(text.encode()) total_lines += lines total_words += words total_chars += chars + output = "" + if not args.l and not args.w and not args.c: - print(lines, words, chars, file) + output += f"{lines:8}{words:8}{chars:8}" else: if args.l: - print(lines, end=" ") + output += f"{lines:8}" if args.w: - print(words, end=" ") + output += f"{words:8}" if args.c: - print(chars, end=" ") - print(file) + output += f"{chars:8}" + + print(output, file) + + if len(args.files) > 1: + output = "" + if not args.l and not args.w and not args.c: - print(total_lines, total_words, total_chars, "total") + output += f"{total_lines:8}{total_words:8}{total_chars:8}" else: if args.l: - print(total_lines, end=" ") + output += f"{total_lines:8}" if args.w: - print(total_words, end=" ") + output += f"{total_words:8}" if args.c: - print(total_chars, end=" ") - print("total") \ No newline at end of file + output += f"{total_chars:8}" + + print(output, "total") \ No newline at end of file