15 lines
471 B
Python
15 lines
471 B
Python
def count_words(filename):
|
|
"""计算一个文件中有多少字"""
|
|
try:
|
|
with open(filename) as f_obj:
|
|
contents = f_obj.read()
|
|
except FileNotFoundError:
|
|
msg = "sorry, the file" + filename + " does not exist"
|
|
print(msg)
|
|
else:
|
|
words = contents.split()
|
|
number_words = len(words)
|
|
print("the file" + filename + "has about " + str(number_words) + " words")
|
|
|
|
filename = 'alice.txt'
|
|
count_words(filename) |