Skip to content

Commit c4d2796

Browse files
committed
handling when path is file
1 parent dc375e6 commit c4d2796

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,17 @@ def counter(item):
118118

119119

120120
for path in args.paths:
121-
with open(path, "r") as f:
122-
content = f.read()
123-
stats = counter(content)
124-
if args.line:
125-
print(f"{stats['lines']} {path}")
126-
elif args.word:
127-
print(f"{stats['words']} {path}")
128-
elif args.char:
129-
print(f"{stats['characters']} {path}")
130-
else:
131-
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
121+
if os.path.isfile(path):
122+
with open(path, "r") as f:
123+
content = f.read()
124+
stats = counter(content)
125+
if args.line:
126+
print(f"{stats['lines']} {path}")
127+
elif args.word:
128+
print(f"{stats['words']} {path}")
129+
elif args.char:
130+
print(f"{stats['characters']} {path}")
131+
else:
132+
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
132133

133134

0 commit comments

Comments
 (0)