【发布时间】:2011-07-21 10:38:39
【问题描述】:
我写了简单的代码进行测试,在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'
【问题讨论】:
-
在 fedora 14 和 python 2.7 上我在 1021 上遇到了这个错误
-
@wiso, +stdin, stdout, stderr 得到 1024 - 我以前在哪里见过这个数字?
-
您应该使用
try..finally或with来安全地关闭文件。对于您的问题:也许您想告诉我们您将要做什么,因为想要您的代码对我来说毫无意义。 -
@gnibber:
ulimit -n给了我 1024。我认为您还需要将/usr/lib64/python2.7/atexit.py和/home/xyz/.pystartup算作打开的文件。 -
如果你在同一个操作系统上用另一种语言尝试这个,你会很快发现这不是 Python 的限制。
标签: python