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
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
import os

# set argument parser
parser = argparse.ArgumentParser(description="Python implementation of cat command")

# to number all lines

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these comments were not here, would it be obvious what these lines of code were doing?

parser.add_argument("-n", action="store_true", help="Number all lines")

# to number non-blank lines
parser.add_argument("-b", action="store_true", help="Number non-blank lines (overrides -n)")

# To raed files
parser.add_argument("files", nargs="+", help="Files to read")

args = parser.parse_args()

if args.n and args.b:
args.n = False

line_number = 1

for file in args.files:
if not os.path.isfile(file):
print(f"cat: {file}: No such file or directory")
continue


with open(file, "r") as f:
for line in f:
if args.b:
if line.strip(): #to number non blank lines only
print(f"{line_number:6}\t{line}", end="")
line_number += 1
else:
print(line, end="") # to print blank line with number

elif args.n:
print(f"{line_number:6}\t{line}", end="") # to print number all lines
line_number += 1
else:
print(line, end ="") # to print content of files
16 changes: 16 additions & 0 deletions implement-shell-tools/ls/ls.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the -1 argument here? Can you see any issues regarding your implementation with -1?

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse
import os

parser = argparse.ArgumentParser(description="dispaly one file per line from ls directory")

parser.add_argument("-1", action="store_true", help="List one file per line")
parser.add_argument("-a", action="store_true", help="Include hidden files (those starting with .)")
parser.add_argument("path", nargs="?", default=".", help="The directory to list (default: current directory)")

args = parser.parse_args()

for filename in sorted(os.listdir(args.path)):
if not args.a and filename.startswith("."):
# skip hidden files unless -a is provided
continue
print(filename)
63 changes: 63 additions & 0 deletions implement-shell-tools/wc/wc.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of issues here:

  • Think about all the combinations of arguments a user might use - do you spot any issues?
  • In your code there seems to be some duplication. Can you find where it is and develop a solution?

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import argparse
import os

parser = argparse.ArgumentParser(description="Python implementation of wc command")

parser.add_argument("-l", action="store_true", help="Print line count")

parser.add_argument("-w", action="store_true", help="Print word count")

parser.add_argument("-c", action="store_true", help="Print byte count")

parser.add_argument("files", nargs="+", help="Files to process")

args = parser.parse_args()

total_lines = 0
total_words = 0
total_bytes = 0

multiple_files = len(args.files) > 1 # to store totals if muliple files

for file in args.files:
if not os.path.isfile(file):
print(f"wc: {file}: No such file or directory")
continue

with open(file, "r", encoding="utf-8") as f:
content = f.read()
lines = content.count("\n")
words = len(content.split())
tbytes = os.path.getsize(file)



total_lines += lines
total_words += words
total_bytes += tbytes

if args.l:
print(f"{lines:} {file}")

elif args.w:
print(f"{words:} {file}")

elif args.c:
print(f"{tbytes:} {file}")

else:
print(f"{lines:>3} {words:>3} {tbytes:>3} {file}") # to print data from per file

#to print total output
if multiple_files:
if args.l:
print(f"{total_lines:} total")

elif args.w:
print(f"{total_words:} total")

else:
print(f"{total_lines:>3} {total_words:>3} {total_bytes:>3} total")