From bef6924ebf2bfb1536a8b33f3274d7ecd30a5e59 Mon Sep 17 00:00:00 2001 From: Abubakar-Meigag <113248042+Abubakar-Meigag@users.noreply.github.com> Date: Fri, 17 Apr 2026 18:41:12 +0100 Subject: [PATCH 1/3] London | 26-SDC-Mar | Beko | Sprint 4 | Implement Shell Tools --- implement-shell-tools/cat/catFile.py | 44 ++++++++++++++++++++++ implement-shell-tools/ls/lsFile.py | 31 ++++++++++++++++ implement-shell-tools/wc/wcFile.py | 55 ++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 implement-shell-tools/cat/catFile.py create mode 100644 implement-shell-tools/ls/lsFile.py create mode 100644 implement-shell-tools/wc/wcFile.py diff --git a/implement-shell-tools/cat/catFile.py b/implement-shell-tools/cat/catFile.py new file mode 100644 index 000000000..79f31fa0b --- /dev/null +++ b/implement-shell-tools/cat/catFile.py @@ -0,0 +1,44 @@ +import sys +import os + +def main(): + argv = sys.argv[1:] + + dash = [arg for arg in argv if arg.startswith('-')] + file_paths = [arg for arg in argv if not arg.startswith('-')] + + show_line_numbers = '-n' in dash + show_non_blank = '-b' in dash + + line_counter = 1 + + for file_path in file_paths: + try: + with open(file_path, 'r', encoding='utf-8') as fs: + content = fs.read() + except (FileNotFoundError, IsADirectoryError): + print(f"cat: {file_path}: No file or directory exists", file=sys.stderr) + sys.exit(1) + + lines = content.split('\n') + + if lines and lines[-1] == '': + lines.pop() + + for line in lines: + is_blank = line.strip() == '' + + if show_non_blank: + if is_blank: + print('') + else: + print(f"{str(line_counter).rjust(6)}\t{line}") + line_counter += 1 + elif show_line_numbers: + print(f"{str(line_counter).rjust(6)}\t{line}") + line_counter += 1 + else: + print(line) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/implement-shell-tools/ls/lsFile.py b/implement-shell-tools/ls/lsFile.py new file mode 100644 index 000000000..cb0c03483 --- /dev/null +++ b/implement-shell-tools/ls/lsFile.py @@ -0,0 +1,31 @@ +import sys +import os + +def main(): + argv = sys.argv[1:] + + dash = [arg for arg in argv if arg.startswith('-')] + paths = [arg for arg in argv if not arg.startswith('-')] + + show_all = '-a' in dash + + target_dir = paths[0] if paths else '.' + + try: + entries = os.listdir(target_dir) + except FileNotFoundError: + print(f"ls: {target_dir}: No such file or directory", file=sys.stderr) + sys.exit(1) + + if show_all: + result = ['.', '..'] + entries + else: + result = [e for e in entries if not e.startswith('.')] + + for entry in result: + print(entry) + +if __name__ == "__main__": + main() + + \ No newline at end of file diff --git a/implement-shell-tools/wc/wcFile.py b/implement-shell-tools/wc/wcFile.py new file mode 100644 index 000000000..cb5ad6b33 --- /dev/null +++ b/implement-shell-tools/wc/wcFile.py @@ -0,0 +1,55 @@ +import sys +import os + +def main(): + argv = sys.argv[1:] + + dash = [arg for arg in argv if arg.startswith('-')] + file_paths = [arg for arg in argv if not arg.startswith('-')] + + for_lines = '-l' in dash + for_words = '-w' in dash + for_bytes = '-c' in dash + + total_lines = 0 + total_words = 0 + total_bytes = 0 + + for file_path in file_paths: + try: + with open(file_path, 'r', encoding='utf-8') as fs: + content = fs.read() + except (FileNotFoundError, IsADirectoryError): + print(f"wc: {file_path}: No file or directory exists", file=sys.stderr) + sys.exit(1) + + lines = len(content.split('\n')) - 1 + words = 0 if content.strip() == '' else len(content.strip().split()) + bytes_count = len(content.encode('utf-8')) + + total_lines += lines + total_words += words + total_bytes += bytes_count + + if for_lines: + print(f"{str(lines).rjust(8)} {file_path}") + elif for_words: + print(f"{str(words).rjust(8)} {file_path}") + elif for_bytes: + print(f"{str(bytes_count).rjust(8)} {file_path}") + else: + print(f"{str(lines).rjust(8)} {str(words).rjust(8)} {str(bytes_count).rjust(8)} {file_path}") + + if len(file_paths) > 1: + if for_lines: + print(f"{str(total_lines).rjust(8)} total") + elif for_words: + print(f"{str(total_words).rjust(8)} total") + elif for_bytes: + print(f"{str(total_bytes).rjust(8)} total") + else: + print(f"{str(total_lines).rjust(8)} {str(total_words).rjust(8)} {str(total_bytes).rjust(8)} total") + +if __name__ == "__main__": + main() + From fa30b02677222ef99aa02cee22a014fb1f5f08a4 Mon Sep 17 00:00:00 2001 From: Abubakar-Meigag <113248042+Abubakar-Meigag@users.noreply.github.com> Date: Wed, 3 Jun 2026 13:12:56 +0100 Subject: [PATCH 2/3] update for code review Implement -1 for the ls file exercises --- implement-shell-tools/ls/lsFile.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/implement-shell-tools/ls/lsFile.py b/implement-shell-tools/ls/lsFile.py index cb0c03483..6abd2aab3 100644 --- a/implement-shell-tools/ls/lsFile.py +++ b/implement-shell-tools/ls/lsFile.py @@ -8,6 +8,7 @@ def main(): paths = [arg for arg in argv if not arg.startswith('-')] show_all = '-a' in dash + one_per_line = '-1' in dash target_dir = paths[0] if paths else '.' @@ -22,8 +23,10 @@ def main(): else: result = [e for e in entries if not e.startswith('.')] - for entry in result: - print(entry) + if one_per_line: + print('\n'.join(result)) + else: + print(' '.join(result)) if __name__ == "__main__": main() From 9421132155c5d8ac4041550cc8f0570e7503fc07 Mon Sep 17 00:00:00 2001 From: Abubakar-Meigag <113248042+Abubakar-Meigag@users.noreply.github.com> Date: Wed, 3 Jun 2026 19:03:11 +0100 Subject: [PATCH 3/3] update removing repetition from the code update for code review --- implement-shell-tools/wc/wcFile.py | 46 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/implement-shell-tools/wc/wcFile.py b/implement-shell-tools/wc/wcFile.py index cb5ad6b33..9b5d4d790 100644 --- a/implement-shell-tools/wc/wcFile.py +++ b/implement-shell-tools/wc/wcFile.py @@ -4,17 +4,29 @@ def main(): argv = sys.argv[1:] + dash = [arg for arg in argv if arg.startswith('-')] file_paths = [arg for arg in argv if not arg.startswith('-')] + for_lines = '-l' in dash for_words = '-w' in dash for_bytes = '-c' in dash - + total_lines = 0 total_words = 0 total_bytes = 0 - + + def print_output(lines, words, bytes_count, label): + if for_lines: + print(f"{str(lines).rjust(8)} {label}") + elif for_words: + print(f"{str(words).rjust(8)} {label}") + elif for_bytes: + print(f"{str(bytes_count).rjust(8)} {label}") + else: + print(f"{str(lines).rjust(8)} {str(words).rjust(8)} {str(bytes_count).rjust(8)} {label}") + for file_path in file_paths: try: with open(file_path, 'r', encoding='utf-8') as fs: @@ -22,34 +34,20 @@ def main(): except (FileNotFoundError, IsADirectoryError): print(f"wc: {file_path}: No file or directory exists", file=sys.stderr) sys.exit(1) - + lines = len(content.split('\n')) - 1 words = 0 if content.strip() == '' else len(content.strip().split()) bytes_count = len(content.encode('utf-8')) - + total_lines += lines total_words += words total_bytes += bytes_count - - if for_lines: - print(f"{str(lines).rjust(8)} {file_path}") - elif for_words: - print(f"{str(words).rjust(8)} {file_path}") - elif for_bytes: - print(f"{str(bytes_count).rjust(8)} {file_path}") - else: - print(f"{str(lines).rjust(8)} {str(words).rjust(8)} {str(bytes_count).rjust(8)} {file_path}") - + + print_output(lines, words, bytes_count, file_path) + if len(file_paths) > 1: - if for_lines: - print(f"{str(total_lines).rjust(8)} total") - elif for_words: - print(f"{str(total_words).rjust(8)} total") - elif for_bytes: - print(f"{str(total_bytes).rjust(8)} total") - else: - print(f"{str(total_lines).rjust(8)} {str(total_words).rjust(8)} {str(total_bytes).rjust(8)} total") + print_output(total_lines, total_words, total_bytes, "total") -if __name__ == "__main__": - main() +if __name__ == "__main__": + main() \ No newline at end of file