递归实现(包含文件夹里有文件夹的情况)
import os
#返回当前文件夹的大小
def getsize(path):
total_size=0
doc_list=os.listdir(path)
for doc in doc_list:
if os.path.isfile(os.path.join(path,doc)):
total_size=total_size+os.path.getsize(os.path.join(path,doc))
else:
total_size=total_size+getsize(os.path.join(path,doc))
return total_size
if __name__ == '__main__':
print("文件总大小为:",getsize(".//"),"字节")