You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
parser.add_argument("files", nargs="+", help="Files to read (shell globs allowed)")
14
+
15
+
returnparser.parse_args()
16
+
17
+
#Uses glob to expand patterns like *.txt.
18
+
defexpand_files(patterns):
19
+
expanded= []
20
+
forpinpatterns:
21
+
matches=glob.glob(p)
22
+
ifmatches:
23
+
expanded.extend(sorted(matches)) # predictable order
24
+
else:
25
+
print(f"wc: {p}: No such file", file=sys.stderr) #Prints an error if a pattern matches nothing.
26
+
returnexpanded
27
+
28
+
deffile_counts(filename):
29
+
try:
30
+
withopen(filename, "rb") asf: #opening file in binary mode to be able to count bytes.
31
+
data=f.read()
32
+
exceptFileNotFoundError:
33
+
print(f"wc: {filename}: No such file", file=sys.stderr)
34
+
returnNone
35
+
36
+
text=data.decode("utf-8", errors="ignore")
37
+
line_count=text.count("\n")
38
+
word_count=len(text.split())
39
+
byte_count=len(data)
40
+
41
+
returnline_count, word_count, byte_count
42
+
43
+
defprint_counts(filename, counts, show_lines, show_words, show_bytes): #args.lines is passed into the parameter show_lines and same for other arguments.
0 commit comments