Skip to content

Commit 60c7deb

Browse files
committed
Updated word count simple extractor with more comments. Renamed the function variable.
1 parent 1d5a551 commit 60c7deb

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import subprocess
22

33

4-
def wordcount(input_file):
5-
result = subprocess.check_output(['wc', input_file], stderr=subprocess.STDOUT)
4+
def wordcount(input_file_path):
5+
"""
6+
This function calculates the number of lines, words, and characters in a text format file.
7+
8+
:param input_file_path: Full path to the input file
9+
:return: Result dictionary containing metadata about lines, words, and characters in the input file
10+
"""
11+
12+
# Execute word count command on the input file and obtain the output
13+
result = subprocess.check_output(['wc', input_file_path], stderr=subprocess.STDOUT)
14+
15+
# Split the output string into lines, words, and characters
616
(lines, words, characters, _) = result.split()
17+
18+
# Create metadata dictionary
719
metadata = {
820
'lines': lines,
921
'words': words,
1022
'characters': characters
1123
}
24+
25+
# Store metadata in result dictionary
1226
result = {
1327
'metadata': metadata
1428
}
29+
30+
# Return the result dictionary
1531
return result

0 commit comments

Comments
 (0)