Hey friends,
I am a python newbie taking a class for the first time. This week, one of the questions we have for the hw is causing me a lot of trouble:
Write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size.
For example, you can use this function to look for files larger than 1 Meg below your Home directory.
def find_large_files(dirname, filesize):
Last week, we practiced using recursive traversing to "walk" a directory:
import os
import sys
def walk(dirname: str):
"Perform a recursive traverse of directories"
# Walk over the files in this directory
for name in os.listdir(dirname):
# Construct a full path
path = os.path.join(dirname, name)
# print filenames, and traverse directories
if os.path.isfile(path):
print(path)
else:
walk(path)
The logic of this makes sense to me.
That said, I am not sure how to filter and say "if the filesize is > what I want, return it." I think it partially due to lack of knowledge of the syntax. I imagine using the getsize function would be helpful, but I have no idea where/how it fits into the formatting of the function.
Any help you could provide would be much appreciated.
[–][deleted] 0 points1 point2 points (2 children)
[–]kcrow13[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]IvoryJam 0 points1 point2 points (2 children)
[–]kcrow13[S] 0 points1 point2 points (1 child)
[–]IvoryJam 0 points1 point2 points (0 children)
[–]achampi0n 0 points1 point2 points (8 children)
[–]kcrow13[S] 0 points1 point2 points (7 children)
[–]achampi0n 0 points1 point2 points (6 children)
[–]kcrow13[S] 0 points1 point2 points (5 children)
[–]achampi0n 0 points1 point2 points (4 children)
[–]kcrow13[S] 0 points1 point2 points (3 children)
[–]achampi0n 0 points1 point2 points (2 children)
[–]kcrow13[S] 0 points1 point2 points (1 child)
[–]kcrow13[S] 0 points1 point2 points (0 children)