eswaat
eswaat

Reputation: 763

Search for file name in full directory python

I have folder(let say master) that have sub folders and in turn they have sub folders.

example

Master
|
|_sub1
     |
     |_sub1_sub1
|
|_sub2
     |
     |_sub2_sub1
     |_filename
|
|_sub3
     |
     |_sub3_sub1

Now I want search for filename. I don't know in which folder it is. So I want to search only in master and if the file exist it should give me folder name in which it found the file. For the example it should give me the output _sub2

Is there any utility in python. I tried exists and isFile but didn't work

Upvotes: 0

Views: 80

Answers (1)

anon582847382
anon582847382

Reputation: 20351

This is perfect for os.walk:

import os

for root, _, files in os.walk('/path/to/master'):
    if 'filename' in files:
        print(os.path.split(root)[1])
        break

Upvotes: 1

Related Questions