close

[Solved] IOError: [Errno 24] Too many open files:

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error IOError: [Errno 24] Too many open files: in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How IOError: [Errno 24] Too many open files: Error Occurs?

Today I get the following error IOError: [Errno 24] Too many open files: in python.

How To Solve IOError: [Errno 24] Too many open files: Error ?

  1. How To Solve IOError: [Errno 24] Too many open files: Error ?

    To Solve IOError: [Errno 24] Too many open files: Error However, it is often easy to limit the number of files that have to be open at the same time. If we look at Stefan Bollman's example, we can easily change that to:

  2. IOError: [Errno 24] Too many open files:

    To Solve IOError: [Errno 24] Too many open files: Error However, it is often easy to limit the number of files that have to be open at the same time. If we look at Stefan Bollman's example, we can easily change that to:

Solution 1

“Too many open files” errors are always tricky – you not only have to twiddle with ulimit, but you also have to check system-wide limits and OSX-specifics. This SO post gives more information on open files in OSX. (Spoiler alert: the default is 256).

However, it is often easy to limit the number of files that have to be open at the same time. If we look at Stefan Bollman’s example, we can easily change that to:

pureResponseNames = ['f'+str(i) for i in range(434)]
outpathDirTest="testCase/"
output_files = [os.path.join(outpathDirTest, fname) + ".txt" for fname in pureResponseNames]

for filename in range(output_files):
    with open(filename, 'w') as f:
        f.write('This is a test of file nr.'+str(i))

Solution 2

In case you can’t close the file for some reasons(e.g. you’re using 3rd party module), you may consider to set based on hard maximum limit instead of predefined hard-coded limit (It will throws ValueError if you try to set hard+1):

import resource
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))

And I want to make it clear that even you manually delete the files created while python process still running, it will still throws such error later.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read