我写了一个简单的测试代码,想知道在Python脚本中可以打开多少个文件:
for i in xrange(2000):
fp = open('files/file_%d' % i, 'w')
fp.write(str(i))
fp.close()
fps = []
for x in xrange(2000):
h = open('files/file_%d' % x, 'r')
print h.read()
fps.append(h)
然后我得到了一个异常
IOError: [Errno 24] Too many open files: 'files/file_509'
try..finally
或with
来安全地关闭文件。至于你的问题:也许你想告诉我们你要做什么,因为你的代码对我来说毫无意义。 - schlamarulimit -n
告诉我是1024。我认为你还需要将/usr/lib64/python2.7/atexit.py
和/home/xyz/.pystartup
也算作已打开的文件。 - Ruggero Turra