import os pathvar =r"E:python30day17ceshi100" lst = os.listdir(pathvar) print(lst) ''' 1.txt 2.txt ceshi200 ''' #(1) 计算文件的大小 size = 0 for i in lst: print(i) pathnew = os.path.join(pathvar,i) if os.path.isfile(pathnew): print(i,"[是文件]") size += os.path.getsize(pathnew) elif os.path.isdir(pathnew): print(i,"[是文件夹]") print(size) #(2) def getallsize(pathvar): size = 0 lst = os.listdir(pathvar) for i in lst: #拼接完整路径 pathnew = os.path.join(pathvar,i) #判断是否是文件 if os.path.isfile(pathnew): size += os.path.getsize(pathnew) #判断是不是文件夹 elif os.path.isdir(pathnew): #递归调用,查看嵌套文件夹里面的内容 size += getallsize(pathnew) return size res = getallsize(pathvar) print(res)