467,074 Members | 869 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,074 developers. It's quick & easy.

Storing of folder structure in SQL DB

Hi All!
Can you please help me with the following problem:
I need to store a copy of local folders structure in MySQL database.
I have chosen the following table structure for that:
------------------------------------------------
| id | id_uplink | folder_name |
------------------------------------------------
id - unique property of each folder.
id_uplink - id of upper level folder is stored here (for example: if
id of c:\test is 1, than id_uplink of c:\test\python equals 1).
folder_name - name of folder.
You see, i dont want to store the path list, but the structure.

The question is how to implement that in Python. I easily made it in C+
+ using recursion. But, unfortunately, I can't figure it out how to
make it in python using os.walk function (or can you recommend smth.
else???). :( Though it looks quite simple, but anyway.

Best Regards,
Segei

Apr 5 '07 #1
  • viewed: 3625
Share:
4 Replies
On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <se************@gmail.comwrote:
Hi All!
Can you please help me with the following problem:
I need to store a copy of local folders structure in MySQL database.
I have chosen the following table structure for that:
------------------------------------------------
| id | id_uplink | folder_name |
------------------------------------------------
id - unique property of each folder.
id_uplink - id of upper level folder is stored here (for example: if
id of c:\test is 1, than id_uplink of c:\test\python equals 1).
folder_name - name of folder.
You see, i dont want to store the path list, but the structure.

The question is how to implement that in Python. I easily made it in C+
+ using recursion. But, unfortunately, I can't figure it out how to
make it in python using os.walk function (or can you recommend smth.
else???). :( Though it looks quite simple, but anyway.

Best Regards,
os.walk should be more than sufficient in your case. You can navigate
the directory structure and at each 'new' directory find its parents
id and assign a new-id to this 'new' directory.

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 !

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.
Apr 5 '07 #2
On 4/5/07, Amit Khemka <kh********@gmail.comwrote:
On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <se************@gmail.comwrote:
<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.
Apr 5 '07 #3
In <ma***************************************@python. org>, Amit Khemka
wrote:
On 4/5/07, Amit Khemka <kh********@gmail.comwrote:
>On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <se************@gmail.comwrote:
<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

[…]

use os.sep instead of '/' !
Or even better `os.path.join()` instead of building the strings with ``+``.

Ciao,
Marc 'BlackJack' Rintsch
Apr 5 '07 #4

Amit Khemka:
On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <se************@gmail.comwrote:
Hi All!
Can you please help me with the following problem:
I need to store a copy of local folders structure in MySQL database.
I have chosen the following table structure for that:
------------------------------------------------
| id | id_uplink | folder_name |
------------------------------------------------
id - unique property of each folder.
id_uplink - id of upper level folder is stored here (for example: if
id of c:\test is 1, than id_uplink of c:\test\python equals 1).
folder_name - name of folder.
You see, i dont want to store the path list, but the structure.

The question is how to implement that in Python. I easily made it in C+
+ using recursion. But, unfortunately, I can't figure it out how to
make it in python using os.walk function (or can you recommend smth.
else???). :( Though it looks quite simple, but anyway.

Best Regards,

os.walk should be more than sufficient in your case. You can navigate
the directory structure and at each 'new' directory find its parents
id and assign a new-id to this 'new' directory.

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 !

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.
Thanks! Your code example was really helpful!

Apr 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Benoit St-Jean | last post: by
4 posts views Thread by Rednelle | last post: by
4 posts views Thread by IkBenHet | last post: by
6 posts views Thread by Kyle Teague | last post: by
3 posts views Thread by teddysnips@hotmail.com | last post: by
reply views Thread by Chuck Anderson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.