Skip to content

LONDON | DONARA BLANC | SHELL TOOLS - PYTHON | SPRINT 4 #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
41 changes: 41 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys

def cat(files, number_all=False, number_nonblank=False):
line_number = 1
for file in files:
try:
with open(file, 'r') as f:
for line in f:
if number_nonblank:
if line.strip() != "":
print(f"{line_number}\t{line}", end='')
line_number += 1
else:
print(line, end='')
elif number_all:
print(f"{line_number}\t{line}", end='')
line_number += 1
else:
print(line, end='')
except FileNotFoundError:
print(f"cat: {file}: No such file or directory", file=sys.stderr)

if __name__ == "__main__":
args = sys.argv[1:]
number_all = False
number_nonblank = False
files = []

for arg in args:
if arg == '-n':
number_all = True
elif arg == '-b':
number_nonblank = True
else:
files.append(arg)

# -b overrides -n
if number_nonblank:
number_all = False

cat(files, number_all, number_nonblank)
27 changes: 27 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import sys

show_all ="-a" in sys.argv

paths = [arg for arg in sys.argv[1:] if not arg.startswith("-")]

if not paths:
paths =["."]

for path in paths:
if os.path.isdir(path):
try:
entries = sorted(os.listdir(path))
for entry in entries:
if show_all or not entry.startswith("."):
print(entry)
except Exception as e:
print(f"ls: cannot access '{path}': {e}")
elif os.path.isfile(path):
print(os.path.basename(path))
else:
print (f"ls:'{path}': no such file or directory")




19 changes: 19 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys
def count_file(filename):
try:
with open (filename, 'r', encoding='utf-8') as f:
content =f.read()

lines = content.splitlines()
words = content.split()
characters = content

print(f"{len(lines)} {len(words)} {len(characters)} {filename}")
except FileNotFoundError:
print(f"wc:{filename}: no such file or directory")


if __name__ == "__main__":
args = sys.argv[1:]
for file in args:
count_file(file)