Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the folder size in a zipfile (Python)

I know that you can get the size in bytes of a file in a ZIP file using the .file_size method But is there any what I can get the size of a folder instead?

Ex:

import zipfile, os

os.chdir('C:\\')    
zp= zipfile.ZipFile('example.zip')

spamInfo = zp.getinfo('spam.txt')    #Here, Instead of a file I'd like to put a folder
spamInfo.file_size

zp.close()
like image 507
tadm123 Avatar asked Oct 10 '16 07:10

tadm123


Video Answer


1 Answers

import zipfile

zp = zipfile.ZipFile("example.zip")

size = sum([zinfo.file_size for zinfo in zp.filelist])
zip_kb = float(size) / 1000  # kB
like image 173
Naresh Chaudhary Avatar answered Sep 20 '22 12:09

Naresh Chaudhary