Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 24dc5b1

Browse files
中尾吏志中尾吏志
authored andcommitted
Add some return value
1 parent 2adfe64 commit 24dc5b1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- cofing: utf-8 -*-
2+
import os
3+
import sys
4+
import collections
5+
6+
script_name = sys.argv[0]
7+
8+
res = {
9+
"total_lines":"",
10+
"total_characters":"",
11+
"total_words":"",
12+
"unique_words":"",
13+
"special_characters":""
14+
}
15+
16+
try:
17+
textfile = sys.argv[1]
18+
with open(textfile) as f:
19+
data = f.read()
20+
21+
res["total_lines"] = data.count(os.linesep)
22+
res["total_characters"] = len(data)
23+
counter = collections.Counter(data.split())
24+
d = counter.most_common()
25+
res["total_words"] = sum([i[1] for i in d])
26+
res["unique_words"] = len([i[0] for i in d])
27+
28+
except IndexError:
29+
print('Usage: %s TEXTFILE' % script_name)
30+
except IOError:
31+
print('"%s" cannot be opened.' % textfile)
32+
33+
print(res)

0 commit comments

Comments
 (0)