Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

dirlist = []
for root, dirs, files in os.walk(PATH_TO_DIRECTORY):
    dirlist += dirs

Should do the trick!

For your revised question, I think what you really need is probably the output of:

Dirdict = {}
for (root, dirs, files) in os.walk (START):
    Dirdict [root] = dirs

You might wish or need some encoding of root with plugin_url(root), this would give you a single dictionary where you could lookup plugin_url(some_path) and get a list of all the directories in that path. What you are doing is creating a list of dictionaries all with a single key. I suspect that you might be after the namedtupple available from collections in python 2.7 and as a builtin in python 3.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top