#!/usr/bin/env python
-*- coding: utf-8 -*-
def count_words(filename):
"""计算一个文件大致包含多少个单词"""
try:
with open(filename) as file_object:
contents=file_object.read()
except FileNotFoundError:
message="Sorry,the file "+filename+" does not exist."
print(message)
else:
#计算文件大致包含多少个单词
words=contents.split()
num_words=len(words)
print("The file "+filename+" has about "+str(num_words)+" words.")
filenames=['alice.txt','siddhartha.txt','moby_dick.txt','little_women.txt']
for filename in filenames:
count_words(filename)
其中siddhartha.txt不在文件夹内,想请问一下这是什么情况~