one method would be to add them to a list and then use sum
sizes = []
for i in a:
dest = '/home/zurelsoft/my_files'
fullname = os.path.join(dest, i) #Get the file_full_size to calculate size
st = int(os.path.getsize(fullname))
f_size = size(st)
sizes.append(f_size)
print sum(sizes)
or you could have a single variable.
sum_size = 0
for i in a:
dest = '/home/zurelsoft/my_files'
fullname = os.path.join(dest, i) #Get the file_full_size to calculate size
st = int(os.path.getsize(fullname))
sum_size += size(st)
print sum_size
or you could keep it in a dictionary....
d = {}
for i in a:
dest = '/home/zurelsoft/my_files'
fullname = os.path.join(dest, i) #Get the file_full_size to calculate size
st = int(os.path.getsize(fullname))
d[i] = size(st)
to get each ones size:
print '\n'.join(['%s: %d' % (k, v) for k, v in d.items()])
to get the sum:
print sum(d.values())
wrapping it all into a function and using a method similar to the one used by Ivo van der Wijk:
def get_file_sizes(parent_dir, files):
import os
return sum([os.path.getsize(os.path.join(parent_dir, f)) for f in files])
calling the function:
a = ['ok.py', 'hello.py']
all_sizes = get_file_sizes('/home/zurelsoft/my_files', a)