diff --git a/implement-shell-tools/cat/cat-python.py b/implement-shell-tools/cat/cat-python.py new file mode 100644 index 00000000..fe8e582d --- /dev/null +++ b/implement-shell-tools/cat/cat-python.py @@ -0,0 +1,49 @@ +import sys +import argparse + +def cat_file(filename, flag): + try: + with open(filename, 'r') as file: + if not flag: + for line in file: + print(line, end="") + + elif flag == "-n": + for index, line in enumerate(file, start=1): + print(f"{index:6}\t{line}", end="") + + elif flag == "-b": + line_number = 1 + for line in file: + if line.strip() == "": + print(line, end="") + else: + print(f"{line_number:6}\t{line}", end="") + line_number += 1 + + except FileNotFoundError: + print(f"cat: {filename}: No such file or directory", file=sys.stderr) + +def main(): + parser = argparse.ArgumentParser(description="Simple cat clone") + + parser.add_argument("filenames", nargs="+", + help="Files to display") + parser.add_argument("-n", action="store_true", + help="Number all output lines") + parser.add_argument("-b", action="store_true", + help="Number non-blank output lines") + + args = parser.parse_args() + + flag = False + if args.n: + flag = "-n" + elif args.b: + flag = "-b" + + for filename in args.filenames: + cat_file(filename, flag) + +if __name__ == "__main__": + main() diff --git a/implement-shell-tools/ls/ls-python.py b/implement-shell-tools/ls/ls-python.py new file mode 100644 index 00000000..3293e565 --- /dev/null +++ b/implement-shell-tools/ls/ls-python.py @@ -0,0 +1,32 @@ +import argparse +import os + +def main(): + parser = argparse.ArgumentParser(description=" ls in Python") + + parser.add_argument("path", nargs="?", default=".", help="Directory to list") + parser.add_argument("-a", action="store_true", help="Show hidden files") + parser.add_argument("-1", dest="one_per_line", action="store_true", + help="List one file per line") + + args = parser.parse_args() + + path = args.path + show_hidden = args.a + one_per_line = args.one_per_line + + entries = os.listdir(path) + + entries.sort() + + if not show_hidden: + entries = [f for f in entries if not f.startswith(".")] + + if one_per_line: + for entry in entries: + print(entry) + else: + print(" ".join(entries)) + +if __name__ == "__main__": + main() diff --git a/implement-shell-tools/wc/wc-python.py b/implement-shell-tools/wc/wc-python.py new file mode 100644 index 00000000..b99c2304 --- /dev/null +++ b/implement-shell-tools/wc/wc-python.py @@ -0,0 +1,65 @@ +import argparse + +def count_file(filename): + + with open(filename, 'r') as file: + content = file.read() + + lines = len(content.splitlines()) + words = len(content.split()) + chars = len(content) + + return (lines, words, chars) + + +def format_output(lines, words, chars, filename, show_l, show_w, show_c): + parts = [] + + if show_l: + parts.append(f"{lines:8}") + if show_w: + parts.append(f"{words:8}") + if show_c: + parts.append(f"{chars:8}") + + parts.append(filename) + + return " ".join(parts) + +def main(): + parser = argparse.ArgumentParser(description="wc imlementation in Python") + + parser.add_argument("files", nargs="+", help="Files to process") + parser.add_argument("-l", action="store_true", help="Show line count") + parser.add_argument("-w", action="store_true", help="Show word count") + parser.add_argument("-c", action="store_true", help="Show character count") + + args = parser.parse_args() + + l = args.l + w = args.w + c = args.c + files = args.files + + if not (l or w or c): + l = w = c = True + + total_lines = total_words = total_chars = 0 + processed_files = 0 + + for filename in files: + result = count_file(filename) + if result: + lines, words, chars = result + print(format_output(lines, words, chars, filename, l, w, c)) + + total_lines += lines + total_words += words + total_chars += chars + processed_files += 1 + + if processed_files > 1: + print(format_output(total_lines, total_words, total_chars, "total", l, w, c)) + +if __name__ == "__main__": + main()