Calculate Total File Size in Specified Path

  • Share this:

Code introduction


This function is used to calculate the total size of all files in a specified path.


Technology Stack : os, json

Code Type : Function

Code Difficulty : Intermediate


                
                    
import os
import sys
import json

def get_files_size(path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if os.path.exists(fp):
                total_size += os.path.getsize(fp)
    return total_size                
              
Tags: