Project Level 2: Intermediate
This project is designed for intermediate learners who know Python fundamentals and are practicing building complete programs.
Project Objective
Create a Python script that searches for files in a specified directory based on a search term. The search tool should allow the user to look for files by name or extension and display matching results with their full file paths.
How the Project Works
(1) The program starts by asking the user to enter a directory path. (2) After the user enters a directory (e.g., /Users/as/Downloads), the program asks the user to enter a search term (e.g., “.png”). (3) Finally, the program lists all the file paths contained in the given directory.
Prerequisites
Required Libraries: os, glob
There is no need to install any libraries since the above libraries are standard libraries.
Required Files: No files are required.
IDE: You can use any IDE on your computer to code the project.
Danger Zone
Once you code the project, compare it with our solution below:
My solution:
"""
Autor: Juan Sebastian Valencia Londoño
file finder
(1) The program starts by asking the user to enter a directory path.
(2) After the user enters a directory (e.g., /Users/as/Downloads),
the program asks the user to enter a search term (e.g., “.png”).
(3) Finally, the program lists all the file paths contained in the given directory.
"""
from pathlib import Path
from os import walk
def find_files():
path = Path(input('Enter the directory to search in: '))
extension = str(input('Enter the search of term or file extension: '))
dir, subdir, files = next(walk(path))
files_extension = []
for file in files:
if file.endswith('.' + extension):
files_extension.append(file)
print('Files matching your search:')
for file in files_extension:
print(Path(dir)/file)
find_files()