Created
April 9, 2018 12:04
-
-
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.
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 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