# 分析多个文本
import chardet
filenames = ['file\pi_digits.txt', 'file\guest.txt', 'file\words.txt']
encod_file = dict()
def get_charset(filename):
with open(filename, 'rb') as f:
t = f.read()
encod_file[filename] = chardet.detect(t)['encoding']
f.close()
for i in filenames:
get_charset(i)
def count_words(filename,encoding):
try:
with open(filename,encoding=encoding) as f:
t = f.read()
except FileNotFoundError:
print("%s not found" % filename)
else:
words = t.split()
num_words = len(words)
print("%s words found in %s" % (num_words,filename))
for k,v in encod_file.items():
count_words(k,v)