0

I have a folder named "main" and in main I have another 10 sub-folders (sub1, sub2,....sub10)
each folder has various CSV files.

I want to get the path if a particular file exists in sub-folders.

for example:
if I am looking for a file named "required_file.csv" and if it exists in folders( sub1 and sub5), my code should return me

paths= ["C\\main\\sub1\\required_file.csv" , "C\\main\\sub5\\required_file.csv"]
Amit
  • 763
  • 1
  • 5
  • 14
  • What have you tried? Please share your code – Moosa Saadat Jul 28 '20 at 16:59
  • 2
    Have you looked at [`os.path.exists()`](https://docs.python.org/2/library/os.path.html#os.path.exists) or [`os.path.isfile()`](https://docs.python.org/2/library/os.path.html#os.path.isfile)? – Tom Jul 28 '20 at 17:00

1 Answers1

1
import os

path_main = 'your_path_here'
filenameToFind = 'your_filename_here'

paths = []
subFolders_list = next(os.walk(path_main))[1]
for subFolder in subFolders_list :
  path = path_main + '/' + subFolder + '/' + filenameToFind # Replace '/' by '\\' if your computer paths are displayed this way
  
  if os.path.isfile(path) : 
    paths.append(path)
RandomGuy
  • 1,076
  • 6
  • 17