python - Python 脚本仅读取 100 个第一个文件
问题描述
我有一个包含 616 个文件的文件夹,但我的脚本只读取前 100 个文件。我需要更改哪些设置才能让它全部读取?这可能是相关的,我正在使用 Anaconda Navigator 的 Jupyter Notebook。
这是我的代码:
import re
import string
from collections import Counter
import os
import glob
def word_count(file_tokens):
for word in file_tokens:
count = Counter(file_tokens)
return count
files_list = glob.glob("german/test/*/negative/*")
print(files_list)
for path in files_list:
corpus, tache, classe, file_name = path.split("\\")
file = open(path, mode="r", encoding="utf-8")
read_file = file.read()
##lowercase
file_clean = read_file.lower()
##tokenize
file_tokens = file_clean.split()
##word count and sort
print(word_count(file_tokens))
解决方案
您可能在系统中达到了一些最大打开文件限制。您可以close
在循环结束时使用每个文件,也可以在循环中使用上下文管理器:
with open(path, mode="r", encoding="utf-8") as file:
....
推荐阅读
- python-3.x - Odoo 12 - 生成和下载 CSV 文件
- bash - 文件不是通过 bash 脚本添加到 zip 中,而是在终端的命令行中工作
- c# - c#如何将标签传递给匿名表单
- javascript - 简单的 addEventlistener 不起作用我不知道如何解决它
- ios - 我试图为我的应用程序实现启动画面,因为它需要从 api 将数据加载到应用程序中
- sql - SQL 筛选列中其他列中的值正在更改的列
- vue.js - 在 Vue 组件中使用匿名函数作为事件处理程序
- python-3.x - 如何使用python将复杂的字典放入表中
- python - 如何重新采样多个数据帧?
- google-slides - 帮助显示在线课程的工具/插件