编写一个python脚本,读取一个目录中的所有文本文件,并统计每个文件中单词的总数。将结果保存到一个名为’word_count.txt‘的文件中
时间: 2024-11-22 15:42:13 浏览: 33
当然,你可以使用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')
阅读全文
相关推荐
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)