python语句整理
2021-05-02 10:28
标签:with open filename style guest try bsp count nbsp object python语句整理 标签:with open filename style guest try bsp count nbsp object 原文地址:https://www.cnblogs.com/merryconei123/p/13204020.html 1 def count_words(filename):
2
3 try:
4 with open(file_name) as file_object:
5 contents = file_object.read()
6 except FileNotFoundError:
7 pass
8 else:
9 file_words = contents.split()
10 file_words_nums = len(file_words)
11 print("The file "+file_name+" has about "+str(file_words_nums)+" words.")
12
13 file_names = [‘guest_book.txt‘,‘book.txt‘,‘guest.txt‘]
14 for file_name in file_names:
15 count_words(file_name)