[Tutor] Sum files' size
Alan Gauld
alan.gauld at btinternet.com
Thu Jul 28 19:48:14 CEST 2011
Susana Iraiis Delgado Rodriguez wrote:
> I want to get the size of 3 files. I already completed this step. Then I
> need to sum the 3 results I got. In order to do it I have the next code:
>
> import os
> file_list = []
> folders = None
> for root, folders, files in os.walk('C:\\'):
> file_list.extend(os.path.join(root,fi) for fi in files if
> (fi.endswith.shp))
> for row, filepath in enumerate(file_list, start=1):
> n = os.path.splitext(filepath)
> p = n[0]+'.prj'
> shx = n[0]+'.shx'
> s = os.path.getsize(filepath)
>
> #Function to get size in humam readable terms:
> def sizeof_fmt(num):
> for x in ['bytes','KB','MB','GB','TB']:
> if num < 1024.0:
> return "%3.1f%s" % (num, x)
This returns a string value
> num /= 1024.0
>
> kb = sizeof_fmt(s)
So kb1 will be astring
> shx1 = os.path.getsize(shx)
> kb2 = sizeof_fmt(shx1)
And so will kb2
> #Finally I want to sum the 3 terms:
> total = kb+kb2+kb3
Where does kb3 come from?
> But the output I got is : 15.5KB108.0bytes169.0bytes
>
> Does anyone have an idea how to fix it?
> Thank you!!
Looks like you are adding the strings. You need to get the
sum then call your format function on the total.
HTH,
Alan G.
More information about the Tutor
mailing list