Level 2: Intermediate
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()
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()