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) 
1
  • 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 Commented Oct 8, 2021 at 8:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.