Python - Get total amount of bytes used by files -
i'm trying total amount of bytes used files.
what i've got far following.
def getsize(self): totalsize = 0 size = 0 root, dirs, files in os.walk(r'c:\\'): files in files: size = os.stat(files).st_size totalsize = totalsize + size
however, when running this, following error pops filenotfounderror: [winerror 2] system cannot find file specified: 'hiberfil.sys'
does know how can fix error , correctly calculate total bytes on disk?
edit: after looking @ more, came following code.
def getsize(): print("getting total system bytes") data = 0 root, dirs, files in os.walk(r'c:\\'): name in files: data = data + getsize(join(root, name)) print("total system bytes", data)
however following error. permissionerror: [winerror 5] access denied: 'c:\\programdata\microsoft\microsoft antimalware\scans\history\cachemanager\mpscancache-1.bin'
this may help:
import os import os.path def getsize(path): totalsize,filecnt = 0,0 root, dirs, files in os.walk(path): file in files: tgt=os.path.join(root,file) if os.path.exists(tgt): size = os.stat(tgt).st_size totalsize = totalsize + size filecnt+=1 return totalsize,filecnt print '{:,} bytes in {:,} files'.format(*getsize('/users/droid'))
prints:
110,058,100,086 bytes in 449,723 files
or, if permission error, use this:
try: size = os.stat(tgt).st_size totalsize = totalsize + size filecnt+=1 except (#permission error type...): continue
Comments
Post a Comment