【发布时间】:2020-11-06 22:59:40
【问题描述】:
我正在使用递归函数来获取每个目录中的文件大小,但得到 TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'。但是我检查了所有子目录中的所有文件,我没有看到任何文件返回 None 类型:
import os
def disk_usage(path):
"""
this function resursiverly check file size in each diretory and return size of
path/directory
provided.
"""
total = os.path.getsize(path)
if os.path.isdir(path):
for file in os.listdir(path):
subdir = os.path.join(path, file)
print(subdir)
total +=disk_usage(subdir)
print ("{} for path {}".format(total, path))
path = "/home/akjha/Documents/Aashutosh_cfengine/"
disk_usage(path)
【问题讨论】:
-
在任何其他语言中,您都会收到警告,告诉您忘记了
return语句,但 python 喜欢假装一切正常,当您忘记显式返回值时,python 会默默返回None.