Skip to content

Instantly share code, notes, and snippets.

@cleoold
Created May 11, 2019 10:47
Show Gist options
  • Save cleoold/0d3b25a9fe92ef2aac1bf62e8f9f9225 to your computer and use it in GitHub Desktop.
Save cleoold/0d3b25a9fe92ef2aac1bf62e8f9f9225 to your computer and use it in GitHub Desktop.
Getting the size of a folder in Python
import os
def getFolderSize(folder):
totalSize = 0
for eachfile in os.listdir(folder):
currPos = os.path.join(folder, eachfile)
if os.path.isfile(currPos):
totalSize += os.path.getsize(currPos)
else:
totalSize += getFolderSize(currPos)
return totalSize
def getFolderSize2(folder):
totalSize = 0
for folderName, _subfolders, filenames in os.walk(folder):
for filename in filenames:
totalSize += os.path.getsize(os.path.join(folderName, filename))
return totalSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment