The Python Oracle

Check if a given directory contains any directory in python

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle3

--

Chapters
00:00 Check If A Given Directory Contains Any Directory In Python
00:41 Accepted Answer Score 11
01:15 Answer 2 Score 2
01:51 Answer 3 Score 1
02:07 Answer 4 Score 0
02:48 Thank you

--

Full question
https://stackoverflow.com/questions/2778...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #directory

#avk47



ACCEPTED ANSWER

Score 11


maybe you want

def folders_in(path_to_parent):
    for fname in os.listdir(path_to_parent):
        if os.path.isdir(os.path.join(path_to_parent,fname)):
            yield os.path.join(path_to_parent,fname)

print(list(folders_in("/path/to/parent")))

this will return a list of all subdirectories ... if its empty then there are no subdirectories

or in one line

set([os.path.dirname(p) for p in glob.glob("/path/to/parent/*/*")])

although for a subdirectory to be counted with this method it must have some file in it

or manipulating walk

def subfolders(path_to_parent):
     try:
        return next(os.walk(path_to_parent))[1]
     except StopIteration:
        return []

 



ANSWER 2

Score 2


I would just do as follows:

#for example
dir_of_interest = "/tmp/a/b/c"

print(dir_of_interest in (v[0] for v in os.walk("/tmp/")))

This prints True or False, depending if dir_of_interest is in the generator. And you use here generator, so the directories to check are generated one by one.

You can break from the walk anytime you want. For example, this brakes is a current folder being walked, has no subdirectories:

for root, dirs, files in os.walk("/tmp/"):
    print(root,len(dirs))
    if not len(dirs): break

Maybe this is in line with what you are after.




ANSWER 3

Score 1


Try this:

#!/usr/local/cpython-3.4/bin/python

import glob
import os

top_of_hierarchy = '/tmp/'
#top_of_hierarchy = '/tmp/orbit-dstromberg'

pattern = os.path.join(top_of_hierarchy, '*')

for candidate in glob.glob(pattern):
    if os.path.isdir(candidate):
        print("{0} is a directory".format(candidate))
        break
else:
    print('No directories found')

# Tested on 2.6, 2.7 and 3.4



ANSWER 4

Score 0


I apparently can't comment yet; however, I wanted to update part of the answer https://stackoverflow.com/users/541038/joran-beasley gave, or at least what worked for me.

Using python3 (3.7.3), I had to modify his first code snippet as follows:

import os

def has_folders(path_to_parent):
   for fname in os.listdir(path_to_parent):
     if os.path.isdir(os.path.join(path_to_parent, fname)):
       yield os.path.join(path_to_parent, fname)

print(list(has_folders("/repo/output")))

Further progress on narrowing to "does given directory contain any directory" results in code like:

import os

def folders_in(path_to_parent):
  for fname in os.listdir(path_to_parent):
    if os.path.isdir(os.path.join(path_to_parent, fname)):
      yield os.path.join(path_to_parent, fname)

def has_folders(path_to_parent):
  folders = list(folders_in(path_to_parent))
  return len(folders) != 0

print(has_folders("the/path/to/parent"))

The result of this code should be True or False