[Tutor] turn a path into nested list

Alan Gauld alan.gauld at btinternet.com
Fri Jan 14 01:21:01 CET 2011


"ingo" <ingoogni at gmail.com> wrote

>> at the os.walk() function for traversing directory trees? It may be 
>> all
>> you need.
>
> Yes, but I'm not confident with it (the more reason to use it you 
> will say :) ).

Quite. A simple tree printout looks like this:

>>> for t in os.walk('Root'):
...    print t
...
And the result is:('Root', ['D1', 'D2', 'D3'], ['FA.txt', 'FB.txt'])
('Root/D1', ['D1-1'], ['FC.txt'])
('Root/D1/D1-1', [], ['FF.txt'])
('Root/D2', [], ['FD.txt'])
('Root/D3', ['D3-1'], ['FE.txt'])
('Root/D3/D3-1', [], ['target.txt'])
>>>That's a lot of work done for just 2 lines of code...Thats taken 
>>>from the OS topic in my tutorial(V2 only as yet)It goes into 
>>>slightly more detail and more examples.But it looks like it will 
>>>sace you a lot of work!
-- Alan GauldAuthor of the Learn to Program web 
sitehttp://www.alan-g.me.uk/ 




More information about the Tutor mailing list