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
30 changes: 30 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import argparse

parser = argparse.ArgumentParser(
prog="cat-command", description="cat shell command in python "
)

parser.add_argument("-n", action="store_true", help="Display all lines numbers")
parser.add_argument("-b", action="store_true", help="Display numbers non-empty lines")
parser.add_argument("path", nargs="+", help="The file to search")

args = parser.parse_args()
number = 1

for file_path in args.path:
with open(file_path, "r") as f:
content = f.readlines()

if args.n:
for line in content:
print(f"{number}\t{line.strip()}")
number += 1
elif args.b:
for line in content:
if line.strip() != "":
print(f"{number}\t{line.strip()}")
number += 1
else:
print("")
else:
print("".join(content))
57 changes: 57 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
import os
import stat
import pwd
import grp
import time

parser = argparse.ArgumentParser(
prog="ls-command", description="ls shell command on python"
)

parser.add_argument("-1", action="store_true", dest="one", help="One file per line")
parser.add_argument(
"-l", action="store_true", help="Display long format description files"
)
parser.add_argument(
"-a", action="store_true", help="Display hidden files along with visible"
)
parser.add_argument("path", nargs="*", default=["."], help="The file to search")


args = parser.parse_args()


def long_format(path, file):
info = os.stat(path)
permissions = stat.filemode(info.st_mode)
size_file = info.st_size
owner = pwd.getpwuid(info.st_uid).pw_name
group = grp.getgrgid(info.st_gid).gr_name
mtime = time.strftime("%b %d %H:%M", time.localtime(info.st_mtime))
print(permissions, size_file, owner, group, mtime, file)


for path in args.path:
if os.path.isfile(path):
file = os.path.basename(path)
if args.l:
long_format(path, file)
else:
print(file)
elif os.path.isdir(path):
files = os.listdir(path)

if not args.a:
files = [file for file in files if not file.startswith(".")]

for file in files:
full_file_path = os.path.join(path, file)

if args.l:
long_format(full_file_path, file)
else:
if args.one:
print(file)
else:
print(file, "\n")
53 changes: 53 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse

parser = argparse.ArgumentParser(
prog="wc-count words, lines, characters", description="wc shell command on python"
)

parser.add_argument("-l", action="store_true", help="Count lines")
parser.add_argument("-w", action="store_true", help="Count words")
parser.add_argument("-c", action="store_true", help="Counts bytes")
parser.add_argument("path", nargs="+", help="The file to search")

args = parser.parse_args()

def calculate_counts(content):

count_lines = content.count("\n")
count_words = len(content.split())
count_bytes = len(content.encode("utf-8"))

return count_lines, count_words, count_bytes

def format_output(count_lines, count_words, count_bytes, file_path):
if not (args.l or args.w or args.c):
print(count_lines, count_words, count_bytes, file_path)
else:
line = ""
if args.l:
line += f"{count_lines} "
if args.w:
line += f"{count_words} "
if args.c:
line += f"{count_bytes} "
line += f"{file_path}"
print(line)

count_total_lines = 0
count_total_words = 0
count_total_bytes = 0

for file_path in args.path:
with open(file_path, "r") as file:
content = file.read()

count_lines, count_words, count_bytes = calculate_counts(content)

count_total_lines += count_lines
count_total_words += count_words
count_total_bytes += count_bytes

format_output(count_lines, count_words, count_bytes, file_path)

if len(args.path) > 1:
format_output(count_total_lines, count_total_words, count_total_bytes, " total")
Loading