Skip to content

Commit 3c8e48b

Browse files
committed
Create 0004.py
1 parent 26933d3 commit 3c8e48b

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

razzl/0004/0004.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
It can caculate the words in the text file
3+
'''
4+
5+
import re
6+
def calculate_words(path):
7+
f = open(path,'r')
8+
lines = f.readlines()
9+
count = 0
10+
for line in lines:
11+
count+=len(re.split('[,.! ?:]',line))#use the re module to split the txt file
12+
return count-len(lines)#the txt file will inlcude the '\n' and '' so sub it
13+
14+
words = calculate_words("C:/Users/razzl/Desktop/1.txt")#in python the '/' can be the path separator in all system
15+
print words

0 commit comments

Comments
 (0)