计算文件大小两种方法 递归和循环

递归:

def list_in(s):
result = 0
lst = os.listdir(s)
for i in lst:
i = s + "/" + i
if os.path.isfile(i):
result = result + os.path.getsize(i)
elif os.path.isdir(i):
ret = list_in(i)
result = result + ret
return result
print(list_in(r"G:/salar"))

循环:
lst = [r"G:/salar", ]
size_sum = 0
while lst :
l_lst = lst.pop()
list_name = os.listdir(l_lst)
for i in list_name:
ret = os.path.join(l_lst, i)
if os.path.isdir(ret):
lst.append(ret)
else:
size_sum += os.path.getsize(ret)
print(size_sum)
posted @ 2020-02-01 19:49  冰灬荷  阅读(231)  评论(0编辑  收藏  举报