有关递归函数,返回值的学习

# 有关函数返回值的问题关键点;谁调用返给谁: 通过递归函数查询文件夹所有文件的大小问题来分析:
# OS模块 :查看一个文件夹下所有文件,这个文件夹有文件夹,不能用walk
# -- coding: UTF-8 --
import os
import sys
#C:UsersAdministratorDesktopExcel
PATH=r'C:UsersAdministratorDesktopExcel'
# 写这个函数必备的知识:
# print(os.getcwd()) # 获取当前路径
# print(os.sep) # 获取系统路径分隔符
# 返回指定目录下所有文件和文件夹(目录):os.listdir(PATH)
# 检验给出的路径是否是以文件: os.path.isfile(PATH)
# 检验是否绝对路径:os.path.isabs()
# 检验给出的路径是否存在: os.path.exists()
# 返回一个路径的目录名和文件名: os.path.split()
#分离扩展名: os.path.splitext()
#获取路径名:os.path.dirname()
#获取文件名:os.path.basename()
#获取文件大小: os.path.getsize(filename)

#os模块:计算一个文件夹所有文件的大小,这个文件夹还要文件夹,不能Walk
# -*- coding: UTF8 -*-
import os
def lookfile(PATH):
sum=0
file = os.listdir(PATH) #获取路径下文件夹和文件


for i in file:

ret=os.path.join(PATH, i)
if os.path.isdir(ret):

ret1=lookfile(ret)
sum+=ret1
else:
fsize = os.path.getsize(ret)
fsize = fsize / float(1024 * 1024)
sum+=fsize


return sum


ret=lookfile(PATH)
print(ret)
import time
#扩展学习
# 1、  '''把时间戳转化为时间: 1479264792 to 2016-11-16 10:53:12'''
# def TimeStampToTime(timestamp):
#       timeStruct = time.localtime(timestamp)
# return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
#
# 2、  '''获取文件的大小,结果保留两位小数,单位为MB'''
#     def get_FileSize(filePath):
#    ilePath = unicode(filePath, 'utf8')
#
#       fsize = os.path.getsize(filePath)
#       fsize = fsize / float(1024 * 1024)
#       return round(fsize, 2)
#
# 3、  '''获取文件的访问时间'''
# def get_FileAccessTime(filePath):
#     filePath = unicode(filePath, 'utf8')
#
#        t = os.path.getatime(filePath)
#        return TimeStampToTime(t)
#
# 4、  '''获取文件的创建时间'''
#     def get_FileCreateTime(filePath):
#       filePath = unicode(filePath, 'utf8')
#
#       t = os.path.getctime(filePath)
#       return TimeStampToTime(t)
#
# 5、  '''获取文件的修改时间'''
#   def get_FileModifyTime(filePath):
#  filePath = unicode(filePath, 'utf8')
#
#       t = os.path.getmtime(filePath)
#       return TimeStampToTime(t)
原文地址:https://www.cnblogs.com/pushuiyu/p/12511114.html