Instructions
Create a simple Python application (Save as w5_firstname_lastname.py) .
Create a Python script that takes two parameters to do the following:-
1) List all files names, size, date created in the given folder
2) Parameter1 = Root Folder name
Parameter2= File size >>> to filter file size ( you can do =, > or <) it is up to you, or a range.
The script should check and validate the folder name, and the size
The script should loop until find all files greater than the specified size in all the sub-folders
3) Use try-exception block (Make sure that the error messages do not reveal any of the application code)
4) Optionally, you can save the list into a file “Results.txt” using the redirection operator, do not create the file in the script.
Submit your week 5 work in w5_firstname_lastname.txt (Please save the file as a text file and upload the text file here for final review.)
Sure! Below is a Python script that meets the requirements specified in the instructions.
Struggling with where to start this assignment? Follow this guide to tackle your assignment easily!
You can save the script as w5_firstname_lastname.py:
Explanation of the Script:
-
Input Parameters:
-
The script takes two command-line parameters:
-
root_folder: The root folder where the search for files starts. -
size_filter: The size filter (in bytes) used to filter files by size. The script checks for files whose size is greater than the specified filter.
-
-
-
Directory Traversal:
-
The script uses the
os.walk()function to traverse all subdirectories of the given root folder.
-
-
File Size and Date:
-
For each file, the script retrieves its size using
os.path.getsize()and its creation date usingos.path.getctime(). -
If the file size is greater than the specified
size_filter, it prints the file name, size, and creation date.
-
-
Error Handling:
-
The script includes error handling with a try-except block to catch any errors such as invalid folder paths or non-integer size filters.
-
-
Command-Line Arguments:
-
The script takes two arguments from the command line and validates them to ensure proper usage.
-
How to Run the Script:
-
Save the script as
w5_firstname_lastname.py(replace “firstname” and “lastname” with your actual name). -
Open the command line or terminal.
-
Run the script with the required parameters:
Example:
This will list all files in the
Documentsfolder (and its subfolders) whose size is greater than 1000 bytes.
Saving Results to a File:
You can redirect the output to a file by running the script with the following command:
This will save the output to a file named Results.txt.
Submission:
For the final review, save your code in a .txt file, which should contain the full Python script. You can name the file w5_firstname_lastname.txt and upload it for review.
Let me know if you need further clarification!
Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount