how to get the file path I have a directory
C: / User / Production It has several folders (10), which also have subfolders. I need to find the test.pdf file in one of the folders.
now my code can find the file only in the folders that are registered in folders.
My code
root = 'C:\\User\\Production' folders = ['ONE','TWO','FREE','FOUR',......] file = 'test.img' folders_comtains_file = [] for folder in folders: filepath = path.join(root,folder,file) if path.isfile(filepath): folders_comtains_file.append(filepath) print(orderName)
import os from os import listdir from os.path import isfile, join path = "C:\\Projects" # Your path search_file = "aidan.ovpn" # File to search for files_found = [] for path in [x[0] for x in os.walk(path)]: onlyfiles = [f for f in listdir(path) if isfile(join(path, f))] files_found.append(path) if search_file in onlyfiles else [] print(files_found) # List of paths where file is found