简体   繁体   English

OSError:[Errno 24]太多打开的文件python,ubuntu

[英]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. 我要编写一个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)

Here I got an error "OSError: [Errno 24] Too many open files: '/home/thisuri/Documents/Re-Train_OCR/Inputs/00009690_B543XY65.png'" 在这里,我得到一个错误“ OSError:[Errno 24]打开的文件太多:'/ 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? 如何解决呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSError:[Errno 24]打开的文件太多 - OSError: [Errno 24] Too many open files OSError: [Errno 24] 打开的文件太多 - OS Mojave - OSError: [Errno 24] Too many open files - OS Mojave OSError: [Errno 24] 使用 Nibabel 打开的文件太多 - OSError: [Errno 24] Too many open files using Nibabel slackclient OSError:[Errno 24]打开的文件太多 - slackclient OSError: [Errno 24] Too many open files Python-OSError 24(打开的文件过多)和共享内存 - Python - OSError 24 (Too many open files) and shared memory __init__ OSError中的文件“ /usr/lib/python3.5/socket.py”,第134行,[Errno 24]打开的文件太多 - File “/usr/lib/python3.5/socket.py”, line 134, in __init__ OSError: [Errno 24] Too many open files Django从一个图像域保存到另一个图像域:OSError:[Errno 24]打开的文件太多 - Django saving from one imagefield to another: OSError: [Errno 24] Too many open files 多处理“OSError:[Errno 24] 打开的文件太多”:如何清理作业和队列? - Multiprocessing "OSError: [Errno 24] Too many open files": How to clean up jobs and queues? OSError:[Errno 24]从终端调用脚本时打开的文件太多 - OSError: [Errno 24] Too many open files when invoking script from terminal OSError:[Errno 24]在Twisted中使用Reactor.run()时打开的文件过多 - OSError: [Errno 24] Too many open files when using reactor.run() in Twisted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM