我在 Windows XP 上使用 Python 2.7。
我的脚本依赖 tempfile.mkstemp 和 tempfile.mkdtemp 创建大量具有以下模式的文件和目录:
_,_tmp = mkstemp(prefix=section,dir=indir,text=True)
<do something with file>
os.close(_)
运行脚本总是会出现以下错误(尽管确切的行号发生了变化等)。脚本尝试打开的实际文件有所不同。
OSError: [Errno 24] Too many open files: 'path\\to\\most\\recent\\attempt\\to\\open\\file'
关于如何调试这个有什么想法吗?另外,如果您需要更多信息,请告诉我。谢谢!
编辑:
这是一个使用示例:
out = os.fdopen(_,'w')
out.write("Something")
out.close()
with open(_) as p:
p.read()
最佳答案
您可能没有在 _
中存储相同的值当您调用os.close(_)
时就像您创建临时文件时一样。尝试分配给命名变量而不是 _
.
如果您可以提供一个非常小的代码片段来演示该错误,这会对您和我们有所帮助。
关于Python操作系统错误: Too many open files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217775/