Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
.venv/
__pycache__/
*.pyc
48 changes: 48 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -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)




1 change: 1 addition & 0 deletions implement-shell-tools/cat/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

42 changes: 42 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -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}")
51 changes: 51 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -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")


Loading