Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import argparse
Comment thread
Nataliia74 marked this conversation as resolved.

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()

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

if args.n:
number = 1
for line in content:
print(f"{number}\t{line.strip()}")
number +=1
elif args.b:
number = 1
for line in content:
if line.strip() !="":
print(f"{number}\t{line.strip()}")
number +=1
else:
print("")
else:
print("".join(content))




# if args.b:
Comment thread
Nataliia74 marked this conversation as resolved.
Outdated
# number=1
# for line in content:
# print

# print(content)
59 changes: 59 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import argparse
Comment thread
Nataliia74 marked this conversation as resolved.
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("-l", action="store_true", help="Display long format description files")
Comment thread
Nataliia74 marked this conversation as resolved.
Outdated
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:
visible_files=[]
Comment thread
Nataliia74 marked this conversation as resolved.
Outdated
for file in files:
if not file.startswith("."):
visible_files.append(file)
files=visible_files

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

if args.l:
long_format(full_file_path, file)
else:
print(file)


Comment thread
Nataliia74 marked this conversation as resolved.
Outdated




38 changes: 38 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import argparse
Comment thread
Nataliia74 marked this conversation as resolved.

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()

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

text = "".join(content)
Comment thread
Nataliia74 marked this conversation as resolved.
Outdated
count_lines = len(content)
count_words = len(text.split())
count_bytes=len(text.encode("utf-8"))

if not (args.l or args.w or args.c):
print (count_lines, count_words, count_bytes, file_path)
if args.l:
print (count_lines, file_path)
if args.w:
print (count_words, file_path)
if args.c:
print (count_bytes, file_path)