diff --git a/.gitignore b/.gitignore index 3c3629e64..2ab7bcc2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ node_modules +.venv/ +__pycache__/ +*.pyc \ No newline at end of file diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..594c6d902 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,48 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat shell tool", + description="making cat tool with oython" +) + +parser.add_argument( + "files", + nargs="+", + help="file to read" +) + +parser.add_argument( + "-n", + "--number", + action="store_true", + help="add number to the lines" +) + +parser.add_argument( + "-b", + "--number_nonblank", + action="store_true", + help="does not number the blank lines" +) + +args = parser.parse_args() + +number_line = 0 +for file in args.files: + with open(file, "r") as f: + lines = f.readlines() + + for line in lines: + if args.number_nonblank: + if line.strip(): + number_line += 1 + print(f"{number_line} {line}", end="") + elif args.number: + number_line += 1 + print(f"{number_line} {line}", end="") + else: + print(line) + + + + diff --git a/implement-shell-tools/cat/requirements.txt b/implement-shell-tools/cat/requirements.txt new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/implement-shell-tools/cat/requirements.txt @@ -0,0 +1 @@ + diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..c07255552 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,42 @@ +import argparse +import os + +parser = argparse.ArgumentParser( + prog="ls shell tool", + description="making ls tool with python" +) + +parser.add_argument( + "-1", + dest="one_per_line", + action="store_true", + help="lists one per line" +) +parser.add_argument( + "-a", + dest="show_hidden", + action="store_true", + help="show files even hidden files" +) +parser.add_argument( + "items", + nargs="*", #defualt to current directory + help="items to list" +) + +args = parser.parse_args() + +paths = args.items if args.items else ['.'] + +for path in paths: + try: + entries = os.listdir(path) + if not args.show_hidden: + entries = [e for e in entries if not e.startswith(".")] + if args.one_per_line: + for entry in entries: + print(entry) + else: + print(" ".join(entries)) + except: + print(f"ls can not access this directory: {path}") diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..740fc3cf5 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,51 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="wc shell tool", + description="making wc tool with python" +) + +parser.add_argument( + "files", + nargs="+", + help="file or files to work on" +) +parser.add_argument( + "-w", + action="store_true", + help="counting words" +) +parser.add_argument( + "-l", + action="store_true", + help="counting lines" +) +parser.add_argument( + "-c", + action="store_true", + help="counting char" +) + +args = parser.parse_args() + +for file in args.files: + try: + with open(file, "r") as f: + content = f.read() + word_count= len(content.split()) + line_count = content.count("\n") + char_count = len(content) + if args.w: + print(f"word count of file {file} is : ", word_count) + elif args.l: + + print(f"line count of file {file} is : ", line_count) + elif args.c: + + print(f"char count of file {file} is : ", char_count) + else: + print(f"line: {line_count}, word: {word_count}, char: {char_count}, {file}") + except FileNotFoundError: + print(f"wc: {file}: file or directory not found") + +