0
I have a question, how to locate a file using python. The goal is to locate a file inside a directory and return all the way from the directory where the file is and if you do not find the file, return None. I created the code below, but it returns all the directories that do not contain the file and also the directory that contains the file. Can anyone check if it is correct or forgot something? Thanks!
import os
diret = "/"
filename = "teste"
def achar_arq():
for roots, dirs, files in os.walk(diret):
if filename in files:
print(os.path.join(roots,filename))
if filename not in files:
print('None')
print(achar_arq())
Does the search need to be recursive in all child directories? If yes, what should happen if more than one file with the same name is found?
– Woss
It must return all the directories that contain a file with this same name, but only these, that is, either return the directories that have the file or return only one None.
– mleas