-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk032.py
42 lines (31 loc) · 1.16 KB
/
k032.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#! /usr/bin/python3
# coding: utf-8
import os.path
import sys
def main():
path = os.path.join(os.path.dirname(__file__), "k03x")
files = sys.argv
outputs = []
for file_name in files[1:-1]:
if not os.path.isfile(os.path.join(path, file_name)):
print(file_name + ": No such file or directory.")
exit(1)
words = []
character_count = 0
line_count = 0
with open(os.path.join(path, file_name), encoding="utf-8", mode="r") as file:
for line in file:
for word in line.split():
words.append(word)
line = line.replace(" ", "")
line = line.replace("\n", "")
character_count += len(line)
line_count += 1
# words = list(set(words))
word_count = len(words)
outputs.append("{0}: {1}文字, {2}語, {3}行".format(file_name, character_count, word_count, line_count))
with open(os.path.join(path, files[-1]), encoding="utf-8", mode="w", newline="\n") as file:
for output in outputs:
file.write(output + "\n")
if __name__ == '__main__':
main()