python 计算文件夹大小

1.获取文件夹的文件

import os
pathvar=r'D:\pycharm project\day17'
lst=os.listdir(pathvar)
print(lst)

2.计算文件的大小(不包含子文件夹大小)

size=0
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):
		print(pathnew,'是文件夹')
print(size)

3.计算文件夹的大小(包含子文件夹的大小)

def func(pathvar):
	size = 0
	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+=func(pathnew)
	return size
print(func(pathvar))

你可能感兴趣的:(python 计算文件夹大小)