Skip to content Skip to sidebar Skip to footer

How To Find The Path Of A Specific Deep Subdirectory With Only The Subdirectory's Name?

I want to move a file into a subdirectory. The subdirectory is within several subdirectories. I have only the name of the parent directory and the name of the subdirectory that I w

Solution 1:

Something like this?

layout:

.
  -> dir1
         -> dir2
                -> first.last-0000

Code:

import os

def find_dir(name, start):
    for root, dirs, files inos.walk(start):
        for d in dirs:
            if d == name:
                returnos.path.abspath(os.path.join(root, d))

subdir_name = 'first.last-0000'
starting_dir = 'C:/users/me/desktop/peopleTest'print(find_dir(subdir_name, starting_dir))

Result:

C:\Users\me\Desktop\peopleTest\dir1\dir2\first.last-0000

Post a Comment for "How To Find The Path Of A Specific Deep Subdirectory With Only The Subdirectory's Name?"