Find lowest level directory

L

loial

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
 
M

Mark Lawrence

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.
 
P

Peter Otten

loial said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top