Skip to content

Commit 76a8647

Browse files
committed
moving bits of code around and updating var so that in the case of dir a total is diplayed to console
1 parent d4cd590 commit 76a8647

File tree

1 file changed

+35
-12
lines changed
  • implement-shell-tools/wc

1 file changed

+35
-12
lines changed

implement-shell-tools/wc/wc.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@
9898
)
9999

100100
parser.add_argument("-l", "--line", dest="line", help="The number of lines in each file", action="store_true")
101-
parser.add_argument("-w", "---word", dest="word", help="The number of words in each file", action="store_true")
102-
parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file")
101+
parser.add_argument("-w", "--word", dest="word", help="The number of words in each file", action="store_true")
102+
parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file", action="store_true")
103103
parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+")
104104

105105
args = parser.parse_args()
@@ -130,24 +130,47 @@ def counter(item):
130130
print(f"{stats['characters']} {path}")
131131
else:
132132
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
133-
elif os.path.isdir(path):
134-
directory = "folder"
135133

136-
for file in os.listdir(directory):
137-
path = os.path.join(directory, file)
134+
total_lines += stats['lines']
135+
total_words += stats['words']
136+
total_characters += stats['characters']
137+
file_count += 1
138+
139+
elif os.path.isdir(path):
140+
for file in os.listdir(path):
141+
file_path = os.path.join(path, file)
138142

139-
if os.path.isfile(path):
140-
with open(path, "r") as f:
143+
if os.path.isfile(file_path):
144+
with open(file_path, "r") as f:
141145
content = f.read()
142146
stats = counter(content)
143147
if args.line:
144-
print(f"{stats['lines']} {path}")
148+
print(f"{stats['lines']} {file_path}")
145149
elif args.word:
146-
print(f"{stats['words']} {path}")
150+
print(f"{stats['words']} {file_path}")
147151
elif args.char:
148-
print(f"{stats['characters']} {path}")
152+
print(f"{stats['characters']} {file_path}")
149153
else:
150-
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
154+
print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}")
155+
156+
total_lines += stats['lines']
157+
total_words += stats['words']
158+
total_characters += stats['characters']
159+
file_count += 1
160+
161+
162+
163+
164+
165+
if file_count > 1:
166+
if args.line:
167+
print(f"{total_lines} total")
168+
elif args.word:
169+
print(f"{total_words} total")
170+
elif args.char:
171+
print(f"{total_characters} total")
172+
else:
173+
print(f"{total_lines} {total_words} {total_characters} total")
151174

152175

153176

0 commit comments

Comments
 (0)