[Tutor] Getting os.walk output into a list :p:
Paradox
paradox at pobox.com
Fri Mar 15 08:13:31 CET 2013
On 03/15/2013 02:31 PM, Dave Angel wrote:
> On 03/15/2013 01:53 AM, Paradox wrote:
>> There is something I can't figure out about the following code (using
>> python 2.7.3):
>>
>> def return_tree_files(rootpath, pattern):
>> for root, dirs, files in os.walk(rootpath):
>> i = [os.path.join(root, filename) for filename in
>> fnmatch.filter(files, pattern)]
>> return i
>>
>> I thought the function would return a list of lists of filenames in the
>> rootpath and all subfolders. Instead I get only the filenames that
>> match the pattern in the rootpath, it doesn't go into the subfolders.
> That's because you returned out of the loop, the first time through.
> Each time through the loop, it describes one directory. Since your
> return after the first one, that's the only directory you'll see.
That makes a lot of sense, thanks so much. I knew the return must be
doing something I didn't understand.
thomas
More information about the Tutor
mailing list