Question: Create a function find_file that accepts a full directory path parameter (i.e., C:Userscs401DesktopDocs) and a file name parameter (i.e., MyFile.txt) that recursively searches all folders

Create a function find_file that accepts a full directory path parameter (i.e., C:\Users\cs401\Desktop\Docs) and a file name parameter (i.e., MyFile.txt) that recursively searches all folders and subfolders under the provided path for the specified filename. When found, the full file pathname should be printed. Note there may be multiple files within a folder structure that match the specified name. If the file is not found, nothing should be printed. If listing the files in a folder results in a PermissionError skip that folder.

CODE IN "PYTHON"

Sample output:

find_file("C:\\Users\\cs401\\Desktop\\Docs", "build.xml")

C:\Users\asd\Desktop\Docs\Art\Arrays\build.xml

C:\Users\asd\Desktop\Docs\comm\Client\build.xml

C:\Users\asd\Desktop\Docs\hola105\build.xml

I have this code so far:

import os

def find_all(file_name, dir_path): file_list = [] for root, dirs, files in os.walk(dir_path): if file_name in files: file_list.append(os.path.join(root, file_name)) return file_list

dir_path = input("Enter directory to check file:") file_name = input("Enter filename:")

print(find_all(file_name,dir_path))

I need help in placing the "PERMISSION ERROR" and also printing in a NEW LINE each time it searches for a filename.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!