Skip to content

Instantly share code, notes, and snippets.

@navidanindya
Created April 9, 2018 12:04
Show Gist options
  • Save navidanindya/f8ddf1c49d15b4ea9740389a88f5807c to your computer and use it in GitHub Desktop.
Save navidanindya/f8ddf1c49d15b4ea9740389a88f5807c to your computer and use it in GitHub Desktop.
Get a folder/directory size in Python 3. Run: [ python DirectorySize.py '/usr/bin' ] (without braces) to get size in '/usr/bin' folder.
import sys
import os
def calculateSize(path):
totalSize = 0
if len(path) > 2:
print('Extra argument given.')
sys.exit(0)
elif len(path) == 1:
path = os.path.abspath(os.getcwd())
else:
path = os.path.abspath(str(path[1]))
for file in os.listdir(path):
totalSize = totalSize + os.path.getsize(os.path.join(path, file))
print(str(totalSize/(1024*1024))+" MBytes")
calculateSize(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment