In: Computer Science
Python: How would I 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, I can use this function to look for files larger than 1 Meg below my Home directory.
CODE:
from os import listdir
from os.path import isfile, join
from os import stat
def getLargeFiles(dirPath, data_size):
#collecting the names of those that are only files
onlyfiles = [f for f in listdir(dirPath) if isfile(join(dirPath, f))]
for file in onlyfiles:
stats = stat(file)
#you can remove this line
print(stats.st_size)
#removing the files that are less in data_size
if stats.st_size <= data_size:
onlyfiles.remove(file)
return onlyfiles
#i have 2 files : 1file 660 bytes and another 597 bytes
print(getLargeFiles("/home/amrit/Projects/Semester/",650))
_____________________________________________________________________________________________________
refer here for output and indentation