-
-
Notifications
You must be signed in to change notification settings - Fork 42
Cape Town| 25-SDC-July | Faith Muzondo | Sprint 4| Implement shell tools python #145
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="cat", | ||
| description="Read, display, and concatenate text files.", | ||
| ) | ||
|
|
||
| parser.add_argument("-n", "--number", action="store_true", help="Number all output lines.") | ||
| parser.add_argument("-b", "--number-nonblank", action="store_true", help="Number non-blank output lines.") | ||
| parser.add_argument("paths", nargs="+", help="The file(s) to read.") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| with open(path, mode='r', encoding='utf-8') as f: | ||
| lines = f.readlines() | ||
| except Exception as err: | ||
| print(f"Error reading file '{path}': {err}") | ||
| continue | ||
|
|
||
| line_num = 1 | ||
|
|
||
| for line in lines: | ||
| should_number = False | ||
| if args.number_nonblank and line.strip(): | ||
| should_number = True | ||
| elif args.number and not args.number_nonblank: | ||
| should_number = True | ||
|
|
||
| if should_number: | ||
| print(f"{line_num:6}\t{line}", end="") | ||
| line_num += 1 | ||
| else: | ||
| print(line, end="") | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import os | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="ls", | ||
| description="List directory contents." | ||
| ) | ||
|
|
||
| parser.add_argument("-1", "--one-per-line", action="store_true", help="List one file per line") | ||
| parser.add_argument("-a", "--all", action="store_true", help="Include hidden files (those starting with .)") | ||
| parser.add_argument("paths", nargs="*", default=["."], help="Directory path(s) to list") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| entries = os.listdir(path) | ||
| except Exception as e: | ||
| print(f"ls: cannot access '{path}': {e}") | ||
| continue | ||
|
|
||
| if not args.all: | ||
| entries = [entry for entry in entries if not entry.startswith(".")] | ||
|
|
||
| entries.sort() | ||
|
|
||
| if args.one_per_line: | ||
| for entry in entries: | ||
| print(entry) | ||
| else: | ||
| print(" ".join(entries)) |
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is some duplication of code here - can you think how to reduce this?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have removed the duplicate code |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| prog="wc", | ||
| description="Counts lines, words, and bytes in text files." | ||
| ) | ||
|
|
||
| parser.add_argument("-l", "--lines", action="store_true", help="Print the line counts") | ||
| parser.add_argument("-w", "--words", action="store_true", help="Print the word counts") | ||
| parser.add_argument("-c", "--bytes", action="store_true", help="Print the byte counts") | ||
| parser.add_argument("paths", nargs="+", help="One or more files to process") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if not (args.lines or args.words or args.bytes): | ||
| args.lines = args.words = args.bytes = True | ||
|
|
||
| total_lines = total_words = total_bytes = 0 | ||
|
|
||
| for path in args.paths: | ||
| try: | ||
| with open(path, "rb") as file: | ||
| content = file.read() | ||
| except Exception as e: | ||
| print(f"wc: {path}: {e}") | ||
| continue | ||
|
|
||
| lines = content.count(b'\n') | ||
| words = len(content.decode('utf-8', errors='ignore').split()) | ||
| byte_count = len(content) | ||
|
|
||
| if args.lines: | ||
| print(f"{lines:>8}", end=" ") | ||
| if args.words: | ||
| print(f"{words:>8}", end=" ") | ||
| if args.bytes: | ||
| print(f"{byte_count:>8}", end=" ") | ||
|
|
||
| print(f"{path}") | ||
|
|
||
| total_lines += lines | ||
| total_words += words | ||
| total_bytes += byte_count | ||
|
|
||
| if len(args.paths) > 1: | ||
| if args.lines: | ||
| print(f"{total_lines:>8}", end=" ") | ||
| if args.words: | ||
| print(f"{total_words:>8}", end=" ") | ||
| if args.bytes: | ||
| print(f"{total_bytes:>8}", end=" ") | ||
| print("total") |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try running the original cat program and comparing your output, do you see any differences?
Also, there is some code duplication in here, can you figure out how to clean that up?