0

For some reason I want this:

a = ['ok.py', 'hello.py']

I want to calculate size of each elements and add all those sizes and store in a single variable:

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)

It does this for every element. How can I add them all?

1
  • 2
    Am i missing something here, or why are you not just adding the size(st)s to the f_size instead of just inserting the value? fsize += size(st) ? Commented Jan 31, 2013 at 11:12

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying to adopt the first one: I get this error: unsupported operand type(s) for +: 'int' and 'str'
the error is not in the code i provided, that error means you are trying to use + between a str and an int there is not even an + operator in the code i wrote. maybe you mean the second one. in which case, it means that size() does not return a number, so you should change size() to return a number, or cast with int()
@InbarRose your code have + operator, in the sum function.
1

You can reduce it to a single sum with generator as follows:

sum(os.path.getsize(os.path.join("/etc", f)) for f in ["passwd", "hosts"])

Basically combining the individual steps you're taking into a single expression that can be passed to sum()

I'm not sure what size() does but you can of course insert that into the expression. Make sure it returns integers and not strings, of course.

2 Comments

only, where did you get /etc, passwd and hosts from? i don't think these paths help the OP. but the answer is indeed correct. dont forget the import os. i would use a dictionary to also store the size for each one. :)
I assume he's smart enough to figure out what /etc, passwd and hosts are in this case. I'm providing a sample that works for everyone (with a decent OS ;) in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.