Question

In: Computer Science

Create a script that take a directory path from the user. What I want you to...

Create a script that take a directory path from the user. What I want you to do is figure out the total size of all files in the directory. You will need to import the os module for this.

  • dirs=os.listdir(DIRECTORY) where DIRECTORY is the direction you want to look at will return a list of all files in that directory.
  • You can then use a for loop to go through dirs much like you went through a range in the previous examples you saw. Start by doing this and printing out the name of each file found.
  • Once you have that working create a total variable and use os.path.getsize(FILE) where FILE is the file's size you want to add the file size to total.
  • Print out the total size for all files in the directory. Note that the result is in bytes.
  • Use an if statement to print the total out in KB if the total is over 1024. If it is not, simply print the total in bytes.
  • Finally add to your program to also calculate the average size of a file in the given directory.

Solutions

Expert Solution

BELOW IS THE SOLUTION TO YOUR PROBLEM:

import os
path=input("Please enter path: ")
dirs = os.listdir(path) #it will return list of names of files in given directory
noOffiles=int(0) #counting number of files
totalSize=int(0) #for calculation of total size of file
for file in dirs: #looping through dirs
print(file) #printing files in directory

for file in dirs:
   if os.path.isfile(file): #checking if given file name is existing file
       totalSize+=os.path.getsize(file)   #calculating file size and adding it in totalSize
       noOffiles+=1 #counting number of files

if(totalSize>1024): # as mentioned in reqirment if size is more than 1024 then convert it in kb
   inKB=totalSize/1024
   print("Total size: {0:.2f} KB".format(inKB))
else:
   print("Total size: "+str(totalSize)+" bytes");

avg=totalSize/noOffiles #calculation avg file size
if(avg>1024):
   inKB=avg/1024
   print("Average size of file: {0:.2f} KB".format(inKB))
else:
   print("Average size: "+str(avg)+" bytes");

Picture of code:

OUTPUT:

IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW.
PLEASE GIVE A THUMBS UP


Related Solutions

Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all...
UNIX/LINUX SCRIPT: Create a named directory and verify that the directory is there by listing all its contents. Write a shell script to validate password strength. Here are a few assumptions for the password string.   Length – a minimum of 8 characters. • Contain alphabets , numbers , and @ # $ % & * symbols. • Include both the small and capital case letters. give a prompt of Y or N to try another password and displays an error...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
Write a script in C that will do the following : 1. Create the directory ZHW3....
Write a script in C that will do the following : 1. Create the directory ZHW3. 2. Verify that the directory is created by display all information related to the directory and not just the name of the directory. 3. Assume that the input from the command is used to validate password strength. Here are a few assumptions for the password string. • Length – minimum of 8 characters. • Contain alphabets , numbers , and @ # $ %...
Write a bash script that... create new user ./finalProject user if a new user is to...
Write a bash script that... create new user ./finalProject user if a new user is to be created ask for the new users information and use it when creating the new user add a new printer ./finalProject printer ask anything you need in order to create the new printer (I.e. name) permissions ./finalProject permissions ask what document and permissions the user wants restarting the computer ./finalProject restart
For this part of the assignment I want you to execute the script CS50_hw_session04b. This will...
For this part of the assignment I want you to execute the script CS50_hw_session04b. This will create a directory called hw_session04 in your home directory. Change directories to hw_session04. - Execute file_1 in hw_session04 - Read file_2 in hw_session04 - Create the file dir_3/name with the output of the whoami command and the first secret code when you execute file_1. Save the output of executing file_1 exactly as shown. There should be two lines in dir_3/name. - Create the file...
Write a Powershell script to create a Windows user account that is a non-admin user. This...
Write a Powershell script to create a Windows user account that is a non-admin user. This assignment assumes that you already have a Windows VM, with the administrator account created.. To complete the assignment, write the PowerShell script, run it on your VM, and submit the script here.
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT