diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..cea347863 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,41 @@ +import argparse +import sys + +# def main(): +parser = argparse.ArgumentParser( + prog="Implment cat command", + description="cat is used to display the content of a file." + ) +parser.add_argument('-n', action='store_true', help='number output lines') +parser.add_argument('-b', action='store_true', help='number non-empty output lines') +parser.add_argument('paths', nargs='+', help="The file path(s) to process") + +args = parser.parse_args() + + +for path in args.paths: + with open(path, 'r') as f: + content= f.read() + + if args.b: + line_num = 1 + lines_arr = [] + + for line in content.splitlines(): + if line.strip() == '': + lines_arr.append(line) + else: + lines_arr.append(f"{line_num:6} {line}") + line_num += 1 + print('\n'.join(lines_arr) + '\n') + + + elif args.n: + lines_arr = [ + f"{i+1:6} {line}" + for i, line in enumerate(content.split('\n')) + ] + sys.stdout.write('\n'.join(lines_arr) + '\n') + + else: + sys.stdout.write(content) diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..21afbf609 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,26 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + prog="Implement ls command in Python", + description="List files in a directory" + ) + +parser.add_argument("-a", action="store_true", help="Display all files include hidden files") +parser.add_argument("-1", action="store_true", help="list one file per line") +parser.add_argument("dir", nargs="?", default=".", help="Directory to list, default curent directory") + +args = parser.parse_args() + +files = os.listdir(args.dir) + +if not args.a: + files = [f for f in files if not f.startswith(".")] +files.sort() + +if args.__dict__["1"]: + for f in files: + print(f) +else: + print(' '.join(files)) + diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..79678e45e --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,75 @@ +import argparse + +def parse_arguments(): + parser = argparse.ArgumentParser( + prog="Implment wc command", + description="Count lines, words, and characters in text files." + ) + + parser.add_argument("paths", nargs='+', type=str, help="The path to process") + parser.add_argument('-l', action="store_true", help='Ouput number of lines') + parser.add_argument('-w', action="store_true", help='Ouput number of words') + parser.add_argument('-c', action="store_true", help='Ouput number of characters') + return parser.parse_args() + + +def wc(path): + with open(path, 'r') as f: + content= f.read() + + line_count = content.count('\n') + word_count = len(content.split()) + char_count = len(content) + return line_count, word_count, char_count + + +def format_output(path, counts, args): + line_count, word_count, char_count = counts + file_output = [] + if not (args.l or args.w or args.c): + file_output = [str(line_count), str(word_count), str(char_count)] + else: + if args.l: + file_output.append(str(line_count)) + if args.w: + file_output.append(str(word_count)) + if args.c: + file_output.append(str(char_count)) + + file_output.append(path) + return(" ".join(file_output)) + + +def main(): + args = parse_arguments() + total_lines = total_words = total_chars = 0 + output_lines =[] + + for path in args.paths: + line_count, word_count, char_count = wc(path) + total_lines += line_count + total_words += word_count + total_chars += char_count + output_lines.append(format_output(path, (line_count, word_count, char_count), args)) + + print("\n".join(output_lines)) + + if len(args.paths) > 1: + output_total =[] + if not (args.l or args.w or args.c): + output_total = [str(total_lines), str(total_words), str(total_chars)] + + else: + if args.l: + output_total.append(str(total_lines)) + if args.w: + output_total.append(str(total_words)) + if args.c: + output_total.append(str(total_chars)) + + output_total.append("total") + print(" ".join(output_total)) + + +if __name__ == "__main__": + main() \ No newline at end of file