Created
May 11, 2019 10:47
-
-
Save cleoold/0d3b25a9fe92ef2aac1bf62e8f9f9225 to your computer and use it in GitHub Desktop.
Getting the size of a folder in Python
This file contains hidden or 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 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