OSError: [Errno 24] Слишком много открытых файлов python, ubuntu - PullRequest
0 голосов
/ 03 декабря 2018

Я собираюсь написать скрипт Python для загрузки изображений из каталога и изменения их размера, поэтому при загрузке изображений из каталога я получаю эту ошибку.

from os import listdir
from PIL import Image as PImage
import split_folders
import os, os.path


def loadImages(path):
    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        img = PImage.open(os.path.join(path,image))
        loadedImages.append(img)
    return loadedImages

path = "./Inputs/"
imgs = loadImages(path)

for img in imgs:
    print(img)

Здесь я получилошибка "OSError: [Errno 24] Слишком много открытых файлов: '/home/thisuri/Documents/Re-Train_OCR/Inputs/00009690_B543XY65.png'"

Ниже приведена трассировка назад;

Traceback (most recent call last):
  File "/home/thisuri/Documents/Re-Train_OCR/2.py", line 6, in <module>
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py", line 221, in imread
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py", line 136, in get_reader
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/format.py", line 687, in search_read_format
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/format.py", line 195, in can_read
  File "/usr/local/lib/python3.6/dist-packages/imageio/plugins/pillow.py", line 104, in _can_read
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 447, in firstbytes
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 456, in _read_first_bytes
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 344, in get_file
OSError: [Errno 24] Too many open files: '/home/thisuri/Documents/Re-Train_OCR/Inputs/00009690_B543XY65.png'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 780, in get_code
  File "<frozen importlib._bootstrap_external>", line 832, in get_data
OSError: [Errno 24] Too many open files: '/usr/lib/python3/dist-packages/apport/__init__.py'

Original exception was:
Traceback (most recent call last):
  File "/home/thisuri/Documents/Re-Train_OCR/2.py", line 6, in <module>
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py", line 221, in imread
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/functions.py", line 136, in get_reader
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/format.py", line 687, in search_read_format
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/format.py", line 195, in can_read
  File "/usr/local/lib/python3.6/dist-packages/imageio/plugins/pillow.py", line 104, in _can_read
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 447, in firstbytes
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 456, in _read_first_bytes
  File "/usr/local/lib/python3.6/dist-packages/imageio/core/request.py", line 344, in get_file
OSError: [Errno 24] Too many open files: '/home/thisuri/Documents/Re-Train_OCR/Inputs/00009690_B543XY65.png'

Как это решить?

1 Ответ

0 голосов
/ 03 декабря 2018

Вам нужно закрыть файлы после их открытия.Попробуйте эту версию:

def loadImages(path):
    imagesList = listdir(path)
    loadedImages = []
    for image in imagesList:
        with open(os.path.join(path, image), 'rb') as i:
            img = PImage.open(i)
            loadedImages.append(img)
    return loadedImages
...