Find lowest level directory

Discussion in 'Python' started by loial, Dec 13, 2012.

  1. loial

    loial Guest

    How can I find the full path of the lowest level directory in a directory structure?

    If there is more than one directory at the lowest level, the first one found will be enough.

    Any help appreciated
    loial, Dec 13, 2012
    #1
    1. Advertising

  2. On 13/12/2012 12:48, loial wrote:
    > How can I find the full path of the lowest level directory in a directory structure?
    >
    > If there is more than one directory at the lowest level, the first one found will be enough.
    >
    > Any help appreciated
    >


    Take a look at the os.path functions. Note that os.path.walk in Python
    2 is called os.walk in Python 3.

    --
    Cheers.

    Mark Lawrence.
    Mark Lawrence, Dec 13, 2012
    #2
    1. Advertising

  3. loial

    Peter Otten Guest

    loial wrote:

    > How can I find the full path of the lowest level directory in a directory
    > structure?
    >
    > If there is more than one directory at the lowest level, the first one
    > found will be enough.


    import os

    def directories(root):
    for path, folders, files in os.walk(root):
    for name in folders:
    yield os.path.join(path, name)

    def depth(path):
    return path.count(os.sep)

    some_folder = ...
    print max(directories(some_folder), key=depth)
    Peter Otten, Dec 13, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    352
    dar7yl
    Mar 11, 2005
  2. Replies:
    3
    Views:
    307
    Paul Tomblin
    Mar 10, 2005
  3. ex laguna
    Replies:
    11
    Views:
    1,384
    Peter Nilsson
    Jul 27, 2003
  4. pabbu
    Replies:
    8
    Views:
    708
    Marc Boyer
    Nov 7, 2005
  5. Anthony
    Replies:
    0
    Views:
    143
    Anthony
    Aug 6, 2004
Loading...

Share This Page