0

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)
Evg
  • 141
  • 3
  • 13
  • ```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``` – Aidan Donnelly Oct 08 '21 at 08:42

1 Answers1

1

I think you're looking for the os.walk method, it should help you find all the files in the sub directories https://www.tutorialspoint.com/python/os_walk.htm