Python:最大递归深度

2024-04-26 18:36:52 发布

您现在位置:Python中文网/ 问答频道 /正文

下面给出一个函数,用于返回其参数的大小之和(可以是单个文件/目录或文件/目录列表)。代码给出了一条错误消息RuntimeError: maximum recursion depth exceeded while calling a Python object,但是我试图对此进行测试。

怎么解决这个问题?

谢谢

苏雷什

#!/usr/bin/python3.1
import os

def fileSizes(f):
    if hasattr(f,'__iter__'):
        return sum(filter(fileSizes,f))
    if os.path.isfile(f):
        return os.path.getsize(f)
    elif os.path.isdir(f):
        total_size = os.path.getsize(f)
        for item in os.listdir(f):
            total_size += fileSizes(os.path.join(f, item))
        return total_size

Tags: 文件path函数代码目录列表参数size