generated from CodeYourFuture/Module-Template
-
-
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
Open
sheetalkharab
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
sheetalkharab:feature/python-implement-shell-tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
132d7cd
python code for ls folder
sheetalkharab c4f47a0
code for cat folder to print content of 1 and all files
sheetalkharab aad36ba
cat folder code upadate for -n flag
sheetalkharab c7e3909
cat folder update code to implement -b flag
sheetalkharab 0c59ef1
wc folder code for count data from muliple files
sheetalkharab 518eafb
wc folder code update to support -l flag
sheetalkharab 40e5125
wc folder code update for -w flag support
sheetalkharab ac8c2de
wc folder code update to support -c flag
sheetalkharab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?