This Python script is designed to search for and delete specified files and folders on your Desktop and Downloads directories. It first lists the found items and then asks for confirmation before deleting them. After the initial deletion, it offers the user the option to specify additional files or folders to delete.
The script performs the following steps:
- Define Search Paths: Sets the Desktop and Downloads directories as the default search paths.
- Define Default File and Folder Names: Specifies a list of default file and folder names to search for.
- Search for Files and Folders: Uses the
os.walk
method to search for the specified files and folders in the search paths. - Display Found Items: Prints the paths of the found files and folders.
- Confirm Deletion: Asks the user if they want to delete the found files and folders.
- Delete Files and Folders: Deletes the files and folders if the user confirms.
- Ask for More Deletions: Prompts the user to enter additional file and folder names to delete and repeats the search and deletion process.
- Import Modules: The script imports the
os
andshutil
modules for file and directory operations. - Define Functions:
find_files(file_names, search_paths)
: Searches for specified file names in the given paths.find_folders(folder_names, search_paths)
: Searches for specified folder names in the given paths.delete_files(file_paths)
: Deletes the specified files.delete_folders(folder_paths)
: Deletes the specified folders.
- Set Search Paths: The script sets the Desktop and Downloads directories as the search paths.
- Specify Default File and Folder Names: Lists the default file and folder names to be searched and deleted.
- Find and Display Items: Searches for and lists the found files and folders.
- User Confirmation: Asks the user if they want to delete the found items.
- Delete Items: Deletes the items if the user confirms.
- Additional Deletions: Prompts the user to specify additional files and folders to delete and repeats the process.
- Run the Script: Execute the script using a Python interpreter.
python delete_files_and_folders.py
- Review Found Items: The script will list the found files and folders.
- Confirm Deletion: Respond to the prompt to confirm whether you want to delete the found items.
- Specify Additional Deletions: If prompted, enter the names of any additional files or folders you want to delete.
$ python delete_files_and_folders.py
Found the following files:
/Users/username/Desktop/Platforms.pdf
/Users/username/Downloads/Courses.pdf
...
Found the following folders:
/Users/username/Desktop/00-Join For Free Courses
...
Do you want to delete these files and folders? (yes/no): yes
Deleted /Users/username/Desktop/Platforms.pdf
Deleted /Users/username/Downloads/Courses.pdf
...
Deleted folder /Users/username/Desktop/00-Join For Free Courses
...
Do you want to delete any more files or folders? (yes/no): yes
Enter the names of files to delete, separated by commas: example.txt, sample.docx
Enter the names of folders to delete, separated by commas: TestFolder, SampleFolder
Found the following files:
/Users/username/Desktop/example.txt
/Users/username/Downloads/sample.docx
...
Found the following folders:
/Users/username/Desktop/TestFolder
/Users/username/Downloads/SampleFolder
...
Do you want to delete these files and folders? (yes/no): yes
Deleted /Users/username/Desktop/example.txt
Deleted /Users/username/Downloads/sample.docx
...
Deleted folder /Users/username/Desktop/TestFolder
Deleted folder /Users/username/Downloads/SampleFolder
...
No more deletions requested.