Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Find lowest level directory

Reply
Thread Tools

Find lowest level directory

 
 
loial
Guest
Posts: n/a
 
      12-13-2012
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
 
Reply With Quote
 
 
 
 
Mark Lawrence
Guest
Posts: n/a
 
      12-13-2012
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.

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      12-13-2012
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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
view file level / directory level security Anthony ASP General 0 08-06-2004 10:46 PM
New Zealand, on the other hand, has one of the lowest growth rates and one of the lowest levels of broadband penetration in the world. punlic NZ Computing 6 05-02-2004 10:28 AM
Where can i find lowest price for lotr:EE? GreenLantern DVD Video 3 12-01-2003 01:43 AM
How to find lowest bit position of mask with macro only ex laguna C Programming 11 07-27-2003 10:43 PM



Advertisments