29.9k views
4 votes
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 catch-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 or by creating the file in the script.

1 Answer

5 votes

Answer:

import sys, time

from os import walk, remove

from os.path import exists, join, getsize, getctime

file_counter = [ ]

folder_name, file_size = argv

isExist = exists( folder_name )

if folder_name == True:

for root, folder, files in walk( folder_name ):

for file in files:

if getsize( join ( root, file ) ) >= file_size:

file_log = [ ]

file_log.append( file )

file_log.append( join ( root, file) )

file_log.append( time.ctime( getctime( file ) ) )

file_counter.append( file_log )

else:

remove ( join ( root, file ) )

Step-by-step explanation:

The python script above output the filename, size and file creation date of any folder passed to it.

User David Espino
by
5.9k points