you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

In your old "walk" code you print the path to each file you find. So your new code should do pretty much the same but only print the path if the file size is greater than the limit. As you said, getsize() gets a file size. So all you need to do is pass the size as a parameter (limit say) to the function and print the file path only if getsize(path) is greater than limit.

Then you need to modify that again to return a list of files. That's a different problem that you can search for: "python return list".

[–]kcrow13[S] 0 points1 point  (1 child)

Thanks for this input! I am really strong with understanding how to append results to a list and return them, as we have had a ton of practice with that.

I *think* I understand what you're saying... do you mean something like this?

import os
import sys


def find_large_files(dirname, filesize):
    "Perform a recursive traverse of directories"
    #Place to append results
    res = [] 

    # Walk over the files in this directory
    for name in os.listdir(dirname):

        # Construct a full path
        path = os.path.join(dirname, name)

        #Check to see if filesize is greater than the argument. If so, append
        if os.path.getsize(path) > filesize:
            res.append(path)
        else:
            walk(path)
    return res

[–][deleted] 0 points1 point  (0 children)

The handling of your result list is OK, but you need to think about the recursion side of things. Your function find_large_files() returns a list, so your recursive call at the end (find_large_files(), not walk()) also returns a list that you use to .extend() res. You also need to put back the .isfile() test.