Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: [Errno 24] Too many open files python , ubuntu

I'm going to write a python script for loading images from a directory and resizing them, so when I'm loading images from a directory I got this error.

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)

Here I got an error "OSError: [Errno 24] Too many open files: '/home/thisuri/Documents/Re-Train_OCR/Inputs/00009690_B543XY65.png'"

Following is trace back;

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'

How to solve this?

like image 925
EverythingNeedToBeKnown Avatar asked Dec 03 '18 06:12

EverythingNeedToBeKnown


1 Answers

You need to close files once you open them. Try this version:

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
like image 179
Burhan Khalid Avatar answered Sep 25 '22 01:09

Burhan Khalid