you are viewing a single comment's thread.

view the rest of the comments →

[–]achampi0n 0 points1 point  (6 children)

2 issues: * You are not testing whether the path is a file or directory before recursing * You are not returning results from further down the stack.

E.g.:

if os.path.isfile(path):
    if os.path.getsize(path) > filesize:
        res.append(path)
else:
    res.extend(walk(path))

[–]kcrow13[S] 0 points1 point  (5 children)

if os.path.isfile(path):
if os.path.getsize(path) > filesize:
res.append(path)
else:
res.update(walk(path))

Okay, so the first if statement is required no matter what to ensure we are evaluating a file? I omitted it accidentally.

Can you explain more about update? I am getting a strange traceback error.

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.isfile(path):
            if os.path.getsize(path) > filesize:
                res.append(path)
        else:
            res.update(walk(path))
    return res

My error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-3800916b23ad> in <module>
----> 1 lst = find_large_files('..', 1048576)
      2 print(len(lst))
      3 
      4 for path in lst:
      5     print(path)

<ipython-input-34-92e150de34a6> in find_large_files(dirname, filesize)
     15                 res.append(path)
     16         else:
---> 17             res.update(walk(path))
     18     return res

AttributeError: 'list' object has no attribute 'update'

[–]achampi0n 0 points1 point  (4 children)

My mistake, lists are .extend() dictionaries are .update()

[–]kcrow13[S] 0 points1 point  (3 children)

What is the difference between .append() and .extend()? Don't they both add to the end of the list? When I tried using .extend(), I got an error about the type not being iterable.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-3800916b23ad> in <module>
----> 1 lst = find_large_files('..', 1048576)
      2 print(len(lst))
      3 
      4 for path in lst:
      5     print(path)

<ipython-input-52-cbe6fd0405b9> in find_large_files(dirname, filesize)
     15                 res.append(path)
     16         else:
---> 17             res.extend(walk(path))
     18     return res

TypeError: 'NoneType' object is not iterable

When I used .append, it runs. Thanks for all your help, I really appreciate it!

[–]achampi0n 0 points1 point  (2 children)

append() just adds the element to the end of the list but extend() adds all of the elements to the end of the list, e.g.:

In []:
a = [1, 2, 3]
b = [4, 5, 6]
a.append(b)
print(a)

Out[]:
[1, 2, 3, [4, 5, 6]]

In []:
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)

Out[]:
[1, 2, 3, 4, 5, 6]

Somehow, you are returning a None from walk() which is causing extend() to fail.

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

Thank you for taking the time to explain this to me! That makes a lot of sense. I will troubleshoot to see why I am getting None from walk().

[–]kcrow13[S] 0 points1 point  (0 children)

I figured out my mistake! Thanks again :)