-
-
Notifications
You must be signed in to change notification settings - Fork 27
London | 25-SDC-July | Andrei Filippov | Sprint 4 | Implement shell tools in Python #142
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 5 commits
5f53628
66d8f5e
573074c
ace3d8e
0ddafe8
8544e56
f9ea264
5ddd011
350c3f2
f8941fb
f654716
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,33 @@ | ||
import os | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="cat", | ||
description="Concatenate and print files", | ||
) | ||
|
||
parser.add_argument("-n",action='store_true', help="Number the output lines, starting at 1") | ||
parser.add_argument("-b", action='store_true', help="Number the non-blank output lines, starting at 1") | ||
parser.add_argument("path", nargs="+", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
for path in args.path: | ||
with open(path) as file: | ||
lines = file.readlines() | ||
line_num = 1 | ||
for line in lines: | ||
lin = line.rstrip('\n') | ||
|
||
if args.b: | ||
if lin =="": | ||
print() | ||
else: | ||
print(line_num, lin) | ||
line_num += 1 | ||
elif args.n: | ||
print(line_num, lin) | ||
line_num += 1 | ||
|
||
else: | ||
print(lin) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="ls", | ||
description="list directory contents", | ||
) | ||
|
||
parser.add_argument("-1",dest="one",action='store_true', help="list one file per line") | ||
parser.add_argument("-a", action='store_true', help="Used to list all files, including hidden files, in the current directory") | ||
parser.add_argument("path", nargs="?", default=".", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
|
||
def arguments_proceeding(files): | ||
data_to_proceed = files | ||
|
||
if args.a: | ||
data_to_proceed = [f for f in files] | ||
data_to_proceed.sort() | ||
data_to_proceed.insert(0, "..") | ||
data_to_proceed.insert(0, ".") | ||
else: | ||
data_to_proceed = [f for f in files if not f.startswith('.')] | ||
data_to_proceed.sort(key=str.lower) | ||
if args.one: | ||
output = [f for f in data_to_proceed] | ||
for f in output: | ||
print(f) | ||
else: | ||
print(" ".join(data_to_proceed)) | ||
|
||
|
||
def path_proceeding(path_argument): | ||
if os.path.isfile(path_argument): | ||
print(path_argument) | ||
elif os.path.isdir(path_argument): | ||
files = os.listdir(path_argument) | ||
arguments_proceeding(files) | ||
|
||
|
||
|
||
path_proceeding(args.path) | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser( | ||
prog="wc", | ||
description="Counts words in a file that contain a particular character", | ||
) | ||
|
||
parser.add_argument("-l",action='store_true', help="The number of lines in each input file is written to the standard output.") | ||
parser.add_argument("-w", action='store_true', help="The number of words in each input file is written to the standard output.") | ||
parser.add_argument("-c", action='store_true', help="The number of bytes in each input file is written to the standard output.") | ||
parser.add_argument("path", nargs="+", help="The path to search") | ||
|
||
args = parser.parse_args() | ||
|
||
if (not args.w and not args.c and not args.l) : | ||
args.w = args.c = args.l = True | ||
total = [] | ||
for path in args.path: | ||
output = [] | ||
if args.l or args.w: | ||
with open(path) as file: | ||
lines = file.readlines() | ||
# lines count | ||
if args.l: | ||
num_lines = len(lines) | ||
output.append(num_lines) | ||
# word count | ||
if args.w: | ||
word_count = 0 | ||
for line in lines: | ||
lin = line.rstrip() | ||
wds = lin.split() | ||
word_count += len(wds) | ||
|
||
output.append(word_count) | ||
|
||
|
||
if args.c: | ||
file_size = os.path.getsize(path) | ||
output.append(file_size) | ||
|
||
if len(args.path) > 1: | ||
total.append(output.copy()) | ||
output.append(path) | ||
string_list = map(str, output) | ||
print(" ".join(string_list)) | ||
|
||
if len(args.path) > 1: | ||
result = [sum(i) for i in zip(*total)] | ||
string_result_list = map(str, result) | ||
print(" ".join(string_result_list), " 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.
When printing out the line numbers, compare how the original cat application works to yours. Do you see any discrepancies?
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.
The only discrepancies I see are in spacing. Is that acceptable, provided the output is correct?