Skip to content

Commit bd4aa9d

Browse files
author
Fatma Degirmenci
committed
Support multiple files and wildcard arguments in wc script
1 parent 6c4eef9 commit bd4aa9d

File tree

1 file changed

+11
-11
lines changed
  • implement-shell-tools/wc

1 file changed

+11
-11
lines changed

implement-shell-tools/wc/wc.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33

44
parser = argparse.ArgumentParser(description="wc command")
5-
parser.add_argument("file", help="File or directory to read")
5+
parser.add_argument("files",nargs="+",help="File(s) or directory to read")
66
parser.add_argument("-l", dest="lines", action="store_true", help="count the number of lines")
77
parser.add_argument("-w", dest="words", action="store_true", help="count the number of words")
88
parser.add_argument("-c", dest="chars", action="store_true", help="count the number of characters")
@@ -29,14 +29,14 @@ def count_file(path):
2929
len(text.split()),
3030
len(text),
3131
)
32+
for file in args.files:
33+
if os.path.isdir(file):
34+
for f in os.listdir(file):
35+
path=os.path.join(file,f)
36+
if os.path.isfile(path):
37+
line_count, word_count, char_count = count_file(path)
38+
print(output(line_count, word_count, char_count, path, args))
3239

33-
if os.path.isdir(args.file):
34-
files = os.listdir(args.file)
35-
for f in files:
36-
path = os.path.join(args.file, f)
37-
if os.path.isfile(path):
38-
line_count, word_count, char_count = count_file(path)
39-
print(output(line_count, word_count, char_count, path, args))
40-
else:
41-
line_count, word_count, char_count = count_file(args.file)
42-
print(output(line_count, word_count, char_count, args.file, args))
40+
else:
41+
line_count, word_count, char_count = count_file(file)
42+
print(output(line_count, word_count, char_count, file, args))

0 commit comments

Comments
 (0)