编写一个python脚本,读取一个目录中的所有文本文件,并统计每个文件中单词的总数。将结果保存到一个名为’word_count.txt‘的文件中
时间: 2024-11-22 19:42:13 浏览: 36
当然,你可以使用Python的os和fileinput库来遍历目录下的所有文本文件,并使用上述提到的`collections.Counter`来统计单词数。下面是相应的脚本:
```python
import os
import re
from collections import Counter
def count_words_in_files(directory):
# 初始化计数器
total_word_count = Counter()
# 遍历目录下的所有文件
for filename in os.listdir(directory):
if filename.endswith(".txt"): # 只处理文本文件
with open(os.path.join(directory, filename), 'r', encoding='utf-8') as file:
# 读取文件内容并去除标点、换行符,然后分词
content = file.read().translate(str.maketrans('', '', string.punctuation + '\n')).lower().split()
# 更新总单词计数
total_word_count.update(content)
# 将结果保存到'word_count.txt'
with open('word_count.txt', 'w', encoding='utf-8') as output_file:
for word, count in total_word_count.items():
output_file.write(f"{word}: {count}\n")
# 调用函数处理指定目录
count_words_in_files('/path/to/your/directory')
阅读全文
相关推荐


















