我写了简单的测试代码,在 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'
原文由 jerboa 发布,翻译遵循 CC BY-SA 4.0 许可协议
打开文件的数量受操作系统限制。在 linux 上你可以输入
看看限制是多少。如果你是 root,你可以输入
现在你的程序将运行正常(作为 root),因为你已经解除了 2048 个打开文件的限制