Python

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:

python
import os
import sys
import time

def list_files(root_folder, size_filter):
try:
# Check if the root folder exists
if not os.path.exists(root_folder):
print("Error: The specified folder does not exist.")
return

# Check if the root folder is a directory
if not os.path.isdir(root_folder):
print("Error: The specified path is not a directory.")
return

# Check if the size filter is valid
try:
size_filter = int(size_filter)
except ValueError:
print("Error: The size filter is not a valid integer.")
return

# Walk through the directory and its subdirectories
for dirpath, dirnames, filenames in os.walk(root_folder):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
file_size = os.path.getsize(file_path)
file_creation_time = os.path.getctime(file_path)
file_creation_date = time.ctime(file_creation_time)

# Compare file size with the filter and print if it matches
if file_size > size_filter:
print(f"File: {filename}")
print(f"Size: {file_size} bytes")
print(f"Created on: {file_creation_date}")
print("-" * 50)

except Exception as e:
print("An unexpected error occurred. Please try again.")

# Main function to handle input parameters
def main():
if len(sys.argv) != 3:
print("Usage: python w5_firstname_lastname.py <root_folder> <size_filter>")
sys.exit(1)

root_folder = sys.argv[1]
size_filter = sys.argv[2]

# Call the function to list files with the given filter
list_files(root_folder, size_filter)

if __name__ == "__main__":
main()

Explanation of the Script:

  1. 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.

  2. Directory Traversal:

    • The script uses the os.walk() function to traverse all subdirectories of the given root folder.

  3. File Size and Date:

    • For each file, the script retrieves its size using os.path.getsize() and its creation date using os.path.getctime().

    • If the file size is greater than the specified size_filter, it prints the file name, size, and creation date.

  4. 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.

  5. Command-Line Arguments:

    • The script takes two arguments from the command line and validates them to ensure proper usage.

How to Run the Script:

  1. Save the script as w5_firstname_lastname.py (replace “firstname” and “lastname” with your actual name).

  2. Open the command line or terminal.

  3. Run the script with the required parameters:

    bash
    python w5_firstname_lastname.py <root_folder> <size_filter>

    Example:

    bash
    python w5_john_doe.py C:/Users/John/Documents 1000

    This will list all files in the Documents folder (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:

bash
python w5_firstname_lastname.py C:/Users/John/Documents 1000 > Results.txt

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!

 

Posted in Uncategorized

Place this order or similar order and get an amazing discount. USE Discount code “GET20” for 20% discount