Search code examples
pythonpython-2.7xbmc

Using os.walk to add to list


I have been trying to add sub-directories to an "items" list and have settled on accomplishing this with the below code.

root, dirs, files = iter(os.walk(PATH_TO_DIRECTORY)).next()
items = [{
     'label': directory, 'path': plugin.url_for('test')
} for count, directory in enumerate(dirs)]

The above works, but it is surprisingly slow. The os.walk is very quick, but the loop is slow for some reason.

I tried to do it all in one go, adding to the "items" list during the os.walk like below

for root, dirs, files in os.walk(PATH_TO_DIRECTORY):

but couldn't quite get the right syntax to add the directories to the list.

Every single example of os.walk I could find online simple did a print of dirs or files, which is fine as an example of its use - but not very useful in the real world.

I am new to python, only just started to look at it today. Could some advise how to get a list like in my first example but without the separate loop?

(I realise it's called a "directory" or something in python, not a list. Let's just call it an array and be done with it... :-)

Thanks


Solution

  • I have no idea what plugin.url_for() does, but you should be able to speed it a bit doit it this way:

    plugin_url_for = plugin.url_for
    _, dirs, _ = iter(os.walk(PATH_TO_DIRECTORY)).next()
    items = [{
         'label': directory, 'path': plugin_url_for('test')
    } for directory in dirs]
    

    I dropped root, files variables as it seems you are not using it, also removed enumerate on dirs as you are not making any use of it. However, put it back if you need it for some weird reason. Please test it and let me know if it helped. I can not test it properly myself for obvious reasons.