Skip to content

Instantly share code, notes, and snippets.

@SteveClement
Created September 20, 2012 12:18
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SteveClement/3755572 to your computer and use it in GitHub Desktop.
Save SteveClement/3755572 to your computer and use it in GitHub Desktop.
Get Directory size in python
import os
def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size
print get_size()
@dmavlov
Copy link

dmavlov commented Feb 21, 2019

...
print('Drictory size: {:,} bytes'.format(total_size).replace(',', ' '))
print('Drictory size: {:,} KB'.format(int(total_size/1024)).replace(',', ' '))
print('Drictory size: {:,} MB'.format(int(total_size/1024**2)).replace(',', ' '))
print('Drictory size: {:,} GB'.format(int(total_size/1024**3)).replace(',', ' '))
print('Drictory size: {:,} TB'.format(int(total_size/1024**4)).replace(',', ' '))

example result

Drictory size: 126 291 748 492 bytes
Drictory size: 123 331 785 KB
Drictory size: 120 441 MB
Drictory size: 117 GB
Drictory size: 0 TB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment