Created
September 20, 2012 12:18
-
-
Save SteveClement/3755572 to your computer and use it in GitHub Desktop.
Get Directory size in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
...
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
import os
total_size = 0
start_path = r"G:\USLAHAM1901"
#get size of current directory
for path, dirs, files in os.walk(start_path):
for f in files:
fp = os.path.join(path,f)
total_size += os.path.getsize(fp)
print("Drictory size:" + str(total_size))
how to translate the result from a huge number to GB or TB? Thank you very much.