From cdb7101074a6d1b325afdb50edd932d3fd9ee216 Mon Sep 17 00:00:00 2001 From: donarbl Date: Thu, 1 May 2025 18:39:30 +0100 Subject: [PATCH 1/3] cat implementation in python to retrieve info from sample files --- implement-shell-tools/cat/cat.py | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 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 00000000..be157515 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,41 @@ +import sys + +def cat(files, number_all=False, number_nonblank=False): + line_number = 1 + for file in files: + try: + with open(file, 'r') as f: + for line in f: + if number_nonblank: + if line.strip() != "": + print(f"{line_number}\t{line}", end='') + line_number += 1 + else: + print(line, end='') + elif number_all: + print(f"{line_number}\t{line}", end='') + line_number += 1 + else: + print(line, end='') + except FileNotFoundError: + print(f"cat: {file}: No such file or directory", file=sys.stderr) + +if __name__ == "__main__": + args = sys.argv[1:] + number_all = False + number_nonblank = False + files = [] + + for arg in args: + if arg == '-n': + number_all = True + elif arg == '-b': + number_nonblank = True + else: + files.append(arg) + + # -b overrides -n + if number_nonblank: + number_all = False + + cat(files, number_all, number_nonblank) From 07176eee341277e2c12dd874fd51e046b50e0a97 Mon Sep 17 00:00:00 2001 From: donarbl Date: Thu, 1 May 2025 19:14:51 +0100 Subject: [PATCH 2/3] ls implemenation in python --- implement-shell-tools/ls/ls.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 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..c4ddc5ea --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,27 @@ +import os +import sys + +show_all ="-a" in sys.argv + +paths = [arg for arg in sys.argv[1:] if not arg.startswith("-")] + +if not paths: + paths =["."] + +for path in paths: + if os.path.isdir(path): + try: + entries = sorted(os.listdir(path)) + for entry in entries: + if show_all or not entry.startswith("."): + print(entry) + except Exception as e: + print(f"ls: cannot access '{path}': {e}") + elif os.path.isfile(path): + print(os.path.basename(path)) + else: + print (f"ls:'{path}': no such file or directory") + + + + From e6bb65996b8fcb6ef72671e4afa909ac119aff48 Mon Sep 17 00:00:00 2001 From: donarbl Date: Fri, 2 May 2025 11:26:36 +0100 Subject: [PATCH 3/3] wc implementation in python --- implement-shell-tools/wc/wc.py | 19 +++++++++++++++++++ 1 file changed, 19 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 00000000..2d1de5ae --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,19 @@ +import sys +def count_file(filename): + try: + with open (filename, 'r', encoding='utf-8') as f: + content =f.read() + + lines = content.splitlines() + words = content.split() + characters = content + + print(f"{len(lines)} {len(words)} {len(characters)} {filename}") + except FileNotFoundError: + print(f"wc:{filename}: no such file or directory") + + +if __name__ == "__main__": + args = sys.argv[1:] + for file in args: + count_file(file) \ No newline at end of file