1. recursive
DEF sum_file_size (File):
su = 0
RET = os.listdir (File) # get all the files in the folder / folder name
for i in RET:
RES = os.path.join (File, i) # the path splicing, the splicing is the absolute path to each file
iF os.path.isdir (RES): # determine whether the folder
name_size = sum_file_size (RES) + su
su + = name_size
elif os.path.isfile (RES ):
FILE_SIZE = os.path.getsize (RES)
SU + = FILE_SIZE
return SU
RES= sum_file_size(r'D:\flin')
print(res)
2. cycle to achieve
= li [R & lt ' D: \ FLIN ' ,] su_ = 0 the while li: # when li circulated to True when A = li.pop () # value of the last deletion list element, the element returns path_liat = os .listdir (a) # get all the files in the folder / folder name for i in path_liat: RET = os.path.join (a, i) # splicing path IF os.path.isdir (RET) : # determines whether the folder, if this path is added to the Li li.append (RET) elif the os.path.isfile (RET): # if the file size of this file will be put adding su_ + = os.path.getsize(ret) print(su_)