Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a function “find_file” that accepts a full directory path parameter (i.e.

ID: 3842979 • Letter: C

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 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:UserssdDesktopDocsArtArraysuild.xml

C:UserssdDesktopDocscommClientuild.xml

C:UserssdDesktopDocshola105uild.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" using "TRY" statement and also printing in a NEW LINE each time it searches for a filename.

---------------------------------------------------------------------------------------------------------------

My second code is where i place the permission error exception but i do not know if it is correct!


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:
            try:
                file_list.append(os.path.join(root, file_name))
            except PermissionError:
                print(file_list)
                return
    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" using "TRY" statement and also printing in a NEW LINE each time it searches for a filename.

Explanation / Answer

Here I have povided the code to print new line in each time it searches for file name.
Regarding the permission error , os.walk method by default handles the permission/IO error by not visiting those directories , you do not require to put any extra logic for that.

Modified Code:

import os
def find_all(file_name, dir_path):
for root, dirs, files in os.walk(dir_path):
if file_name in files:
print(os.path.join(root, file_name))
dir_path = input("Enter directory to check file:")
file_name = input("Enter filename:")
find_all(file_name,dir_path)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote