Python 脚本仅读取 100 个第一个文件

Python Script Only Reads 100 First Files

我有一个包含 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))

您是否尝试打印 files_list 变量的长度并检查它是 616 还是 100?

print(len(files_list))

您可能达到了系统中的最大打开文件数限制。您可以 close 循环结束时的每个文件,或者在循环中使用上下文管理器:

with open(path, mode="r", encoding="utf-8") as file:
    ....