Skip to content

Instantly share code, notes, and snippets.

@hoanbka
Created January 24, 2017 08:38
Show Gist options
  • Save hoanbka/95c80c4b1b87d47624859a1f36281e3c to your computer and use it in GitHub Desktop.
Save hoanbka/95c80c4b1b87d47624859a1f36281e3c to your computer and use it in GitHub Desktop.
Calculate the size of the folder using Python
import os
res = []
def readFolder(file):
if os.path.isfile(file):
res.append(os.path.getsize(file))
else:
arr = os.listdir(file)
for element in arr:
child = file + "\\" + element
if os.path.isfile(child):
res.append(os.path.getsize(child))
else:
readFolder(child)
return sum(res)
# path = 'D:\English\English_Collocations_in_Use_Advanced.pdf'
# path = 'D:\English\CDIeltsband5.5-6.5.rar'
path = 'D:\English'
# print(readFolder(path))
arr = readFolder(path)
print(arr)
# print(os.path.isfile("D:\English\TOEIC\900A\Test10 - Tu luyen TOEIC 900.mp3"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment