-
-
Notifications
You must be signed in to change notification settings - Fork 27
Glasgow | 25-SDC-July | Sheetal Kharab | Sprint 4 | implement-shell tools in python #153
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
132d7cd
c4f47a0
aad36ba
c7e3909
0c59ef1
518eafb
40e5125
ac8c2de
dfaa6ea
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,42 @@ | ||
import argparse | ||
import os | ||
|
||
# set argument parser | ||
parser = argparse.ArgumentParser(description="Python implementation of cat command") | ||
|
||
# to number all lines | ||
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 |
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. What is the purpose of the -1 argument here? Can you see any issues regarding your implementation with -1? 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. The argument is defined but unused; currently, the program always prints one file per line, so -1 doesn’t change anything. 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. 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) |
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 are a couple of issues here:
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. 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") | ||
|
||
|
||
|
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.
If these comments were not here, would it be obvious what these lines of code were doing?
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.
Thank you, Yes these lines are self explainatory and comment are not required here.