Question

In: Computer Science

my_list = {} Dictionary holding all the lists. s_files = [] # Files with size UNDER...

my_list = {} Dictionary holding all the lists. s_files = [] # Files with size UNDER 50 KB. m_files = [] # Files with size AT LEAST 50 KB and UNDER 5 MB . l_files = [] # Files with size AT LEAST 5 MB, no upper limit. directories = [] # Directories, regardless of size. Write a program using python that loops over each file in a specified directory and checks the size of each file.Create 2-tuple with the filename and size, should append the 2-tuple to a list, and then store all the lists in a dictionary according to the size

Solutions

Expert Solution

import os

my_list = {} #Dictionary holding all the lists.
s_files = [] # Files with size UNDER 50 KB.
m_files = [] # Files with size AT LEAST 50 KB and UNDER 5 MB
l_files = [] # Files with size AT LEAST 5 MB, no upper limit.
directories = [] # Directories, regardless of size.
# enter the path of your directory here
directory = r'E:\test'
Unit = ["Byte","KB","MB","GB","TB"]
for filename in os.listdir(directory):
    c = 0
    # this will get the file size in bytes
    b = os.path.getsize(os.path.join(directory, filename))
    # this will convert bytes to MB.... GB... etc
    while b > 1024:
        b = b/1024
        c+=1
    size = "{:.1f}".format(b)+ " " + Unit[c]
    tup = (filename,size)
    # to check print output of each tuple
    print(tup)
    directories.append(tup)
    if Unit[c]=="Byte" or Unit[c]=="KB" and b<50 :
        s_files.append(tup)
    elif (Unit[c]=="KB" and b>=50) or (Unit[c]=="MB" and b<5) :
        m_files.append(tup)
    else:
        l_files.append(tup)
my_list["under 50 KB"]= s_files
my_list["between 50 KB and 5 MB"]= m_files
my_list["above 5 MB"]= l_files

This code will work for the above program

Hope this helpful

Please please please upvote as it will helps me alot

please dont downvote


Related Solutions

PYTHON...... Working with lists, functions, and files Objective: Work with lists Work with lists in functions...
PYTHON...... Working with lists, functions, and files Objective: Work with lists Work with lists in functions Work with files Assignment: Part 1: Write a program to create a text file which contains a sequence of test scores. Ask for scores until the user enters an empty value. This will require you to have the scores until the user enters -1. After the scores have been entered, ask the user to enter a file name. If the user doesn’t not enter...
(a) Holding all other values constant, increasing the sample size increases the power of a hypothesis...
(a) Holding all other values constant, increasing the sample size increases the power of a hypothesis test. (True/False) (b) If a 95% confidence interval (CI) for μ is (52, 58), then 95% of the population values are between 52 and 58. (True/False) (c) If the population distribution (X dist.) is normal, then the sampling distribution of X̅ is normal for a random sample of any size. (True/False) (d) A 95% confidence interval for μ was computed to be (30, 35)....
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
This assignment involves the use of text files, lists, and exception handling and is a continuation...
This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>> Enter gender (boy/girl): boy...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience...
[Python programming] Functions, lists, dictionary, classes CANNOT BE USED!!! This assignment will give you more experience on the use of: 1. integers (int) 2. floats (float) 3. conditionals(if statements) 4. iteration(loops) The goal of this project is to make a fictitious comparison of the federal income. You will ask the user to input their taxable income. Use the income brackets given below to calculate the new and old income tax. For the sake of simplicity of the project we will...
3.13 LAB: Extracting Passwords (files and lists) The Linux operating system is a very popular server...
3.13 LAB: Extracting Passwords (files and lists) The Linux operating system is a very popular server OS. A network administrator has to protect the login/password files stored on the servers. In Linux there are two important files: /etc/passwd And it contains rows that look like this: root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin user1:x:15:51:User One:/home/user1:nologin user2:x:15:51:User One:/home/user1:nologin user3:x:15:51:User One:/home/user1:nologin This file contains login information. It's a list of the server's accounts that has userID, groupID, home directory, shell and more info....
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each...
PYTHON Computer Science Objectives Work with lists Work with functions Work with files Assignment Write each of the following functions. The function header must be implemented exactly as specified. Write a main function that tests each of your functions. Specifics In the main function ask for a filename and fill a list with the values from the file. Each file should have one numeric value per line. This has been done numerous times in class. You can create the data...
Their price lists are shown in the table Ordering cost is $55, and annual holding cost per unit is $4
M.P. VanOyen Manufacturing has gone out on bid for a regulator component. Expected demand is 725 units per month. The item can be purchased from either Allen Manufacturing or Baker Manufacturing. Their price lists are shown in the table Ordering cost is $55, and annual holding cost per unit is $4    Allen Mfg.Baker Mfg.QuantityUnit PriceQuantityUnit Price1-499$16.00  1-399$16.10  500-99915.50400-79915.601000+15.00800+15.10a) What is the economic order quantity if price is not a consideration? b) Which supplier, based on all options with regard to discounts,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT