Question

In: Computer Science

Python: How would I write a function that takes a directory and a size in bytes,...

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.

Solutions

Expert Solution

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


Related Solutions

Python: How would I write a function that takes a URL of a webpage and finds...
Python: How would I write a function that takes a URL of a webpage and finds the first link on the page? The hint given is the function should return a tuple holding two strings: the URL and the link text.
To implement a recursive algorithm to calculates the total directory size (in bytes) of the portion...
To implement a recursive algorithm to calculates the total directory size (in bytes) of the portion of the file system rooted at the given path. Program will rely on the following methods of the class: new File(pathString) or new File(parentFile,childString) file.length() returns the immediate disk usage(measured in bytes) file.isDirectory() Returns true if the File instance represents a directory; false otherwise file.list() Return an array of strings designating the names of all entries within the given directory
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
Python: How would I write a function using list comprehensions to list of integers into two...
Python: How would I write a function using list comprehensions to list of integers into two halves with the evens first, followed by odds? And on top of that, the numbers should be listed in their original order.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is...
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is my code: #importing sqlite and pandas import sqlite3 import pandas as pd #goal print("Welcome! The goal of this assigment is to create a database to find meanings and synonyms for given phrases.") #Connecting database conn = sqlite3.connect("keilavaldez.db") #Creating cursor to remove existing specified tables cur = conn.cursor() #creating tables cur.execute("CREATE TABLE Synsets([SynsetID] INTEGER,[Definition] text)") cur.execute("CREATE TABLE Phrases([SynsetID] INTEGER,[phrase] text)") conn.commit() #opening and reading table...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT