Skip to content
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?

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, Yes these lines are self explainatory and comment are not required here.

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?

Copy link
Author

Choose a reason for hiding this comment

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

The argument is defined but unused; currently, the program always prints one file per line, so -1 doesn’t change anything.

Copy link
Author

Choose a reason for hiding this comment

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

Hello @LonMcGregor, sorry for late response. when i am checking these file I found about these comments. so I made some changes acc to your suggestions. Sorry once again.

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)
62 changes: 62 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?

Copy link
Author

Choose a reason for hiding this comment

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

yes, you are right. The code only handles one flag at a time because it uses if…elif…else, so combinations like -l -w don’t work. Also, the printing logic is duplicated for per-file and total counts. I just made some chnae sacc to your suggestions. Thank you.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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

# Helper function to print counts in wc style
def print_counts(lines, words, bytes_, filename):
output = []
if args.l:
output.append(str(lines))
if args.w:
output.append(str(words))
if args.c:
output.append(str(bytes_))

# If no flags are given, print all three counts
if not output:
output = [f"{lines:>7}", f"{words:>7}", f"{bytes_:>7}"]

print(" ".join(output), filename)

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

# Print counts per file
print_counts(lines, words, tbytes, file)

#to print total output
if multiple_files:
print_counts(total_lines, total_words, total_bytes, "total")



Loading