1

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 want the file to go into. I do not know the subdirectories in-between the parent directory and destination subdirectory and need to find where that subdirectory is with its absolute path so I can then move the file into that subdirectory.

I have tried os.path.isdir() and os.path.exists() to search for my subdirectory, but the search does not seem to search into all subdirectories.

    import os

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

subdir_name = 'Shanahan,Austin-1234'
starting_dir = r'C:\Users\austin.shanahan\Desktop\PeopleTest'

print(find_dir(subdir_name, starting_dir))   # returns "None"  

***There is a subdirectory called Shanahan,Austin-1234 deep in the directory PeopleTest. There are two subdirectories in-between the directory PeopleTest and the subdirectory Shanahan,Austin-1234. I need to find Shanahan,Austin-1234 within all of that and output the absolute path.

austin
  • 13
  • 5
  • 1
    You need to recursively search the subdirectories yourself until either the item searched is found or there are no subdirectories to search. – gabriel.hayes Nov 11 '19 at 17:08
  • what OS are you running if debian you can use the `call("find filename_you_are_searching.for")` and then parse the result – ThunderHorn Nov 11 '19 at 17:10
  • 1
    @ThunderHorn Given that Python is a cross-platform language, it wouldn't make sense to use platform-specific code for a non-platform-specific problem. – gabriel.hayes Nov 11 '19 at 17:11

1 Answers1

3

Something like this?

layout:

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

Code:

import os

def find_dir(name, start):
    for root, dirs, files in os.walk(start):
        for d in dirs:
            if d == name:
                return os.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
tgikal
  • 1,667
  • 1
  • 13
  • 27
  • 1
    And possibly `break` after finding the file, if that's desired :) – AKX Nov 11 '19 at 17:24
  • @AKX I'd normally do it in a function, so a return instead of a print. – tgikal Nov 11 '19 at 17:24
  • Sure. Point being it's no use walking through the rest of the tree. – AKX Nov 11 '19 at 17:25
  • 1
    @tgikal - yes that's the logic i'm looking for. my code doesn't output a result though. I'll edit my question to show what I have at this point – austin Nov 11 '19 at 17:33
  • @austin The latest edit should work for that layout. (side note `dir` is a python keyword and should be avoided.) – tgikal Nov 11 '19 at 17:43
  • @tgikal thanks for that side note. I have used your bit of code but it returns "None" for some reason. Could this be because the code is searching for d in dirs but not in subdirs? That is possibly a stupid question sorry in advance. I'm very new to this and trying to automate a big task at work – austin Nov 11 '19 at 17:59
  • @austin Edit your question with exactly what you are trying, and we'll see if if we can't figure out what isn't working correctly. – tgikal Nov 11 '19 at 18:03
  • @tgikal thank you, my question is up-to-date. C:\Users\austin.shanahan\Desktop\PeopleTest\S\SE-SH\Shanahan,Austin-1234 - this is the output i would be looking for. S and SE-SH are the two in-between subdirs. However, I need my code to search through them automatically. Hope that makes sense – austin Nov 11 '19 at 18:15
  • @austin you still have `for root, dirs, files in os.walk("."):` instead of `for root, dirs, files in os.walk(start):`, sorry about that, was a late edit on my part, managed to catch the code in that minute I had it like that. – tgikal Nov 11 '19 at 18:18