Storing of folder structure in SQL DB

Amit Khemka khemkaamit at gmail.com
Thu Apr 5 08:35:37 EDT 2007


On 4/5/07, Amit Khemka <khemkaamit at gmail.com> wrote:
> On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <sergei.minayev at gmail.com> wrote:
<snip>
> An Example:
>
> import os
> root='/my/root/directory'
> id =0
> tree={root:(-1, id)}
> id+=1
> for path, dirs, files in os.walk(root):
>         for dir in dirs:
>                 if not tree.has_key(path+'/'+dir):
>                         tree[path+'/'+dir]=(tree[path][1], id)
>                         id+=1
>
> It stores ids as a tuple (parent_id, id) in a dictionary(tree). Should
> be straight forward to modify to your requirements. Also you can make
> the following code more efficient by saving/caching some lookups !

use os.sep instead of '/' !

 Cheers,

-- 
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.



More information about the Python-list mailing list