Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Storing of folder structure in SQL DB

Reply
Thread Tools

Storing of folder structure in SQL DB

 
 
Sergei Minayev
Guest
Posts: n/a
 
      04-05-2007
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

 
Reply With Quote
 
 
 
 
Amit Khemka
Guest
Posts: n/a
 
      04-05-2007
On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Amit Khemka
Guest
Posts: n/a
 
      04-05-2007
On 4/5/07, Amit Khemka <> wrote:
> On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <> 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.
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      04-05-2007
In <mailman.6025.1175776544.32031.python->, Amit Khemka
wrote:

> On 4/5/07, Amit Khemka <> wrote:
>> On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <> 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
>>
>> […]

>
> use os.sep instead of '/' !


Or even better `os.path.join()` instead of building the strings with ``+``.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Sergei Minayev
Guest
Posts: n/a
 
      04-06-2007

Amit Khemka:
> On 5 Apr 2007 04:58:22 -0700, Sergei Minayev <> wrote:
> > 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!

 
Reply With Quote
 
santhosh.sweetmemory@gmail.com
Guest
Posts: n/a
 
      09-21-2012
folderid name parentid

1 cricket 0
2 india 1
3 sachin 2
4 tennis 0
5 saniamirza 4

i need coding for this table..folder id 'll automatically populate..
 
Reply With Quote
 
santhosh.sweetmemory@gmail.com
Guest
Posts: n/a
 
      09-21-2012
On Friday, September 21, 2012 11:57:05 AM UTC+5:30, santhosh.s...@gmail.com wrote:
> folderid name parentid
>
>
>
> 1 cricket 0
>
> 2 india 1
>
> 3 sachin 2
>
> 4 tennis 0
>
> 5 saniamirza 4
>
>
>
> i need coding for this table..folder id 'll automatically populate..


in asp.net or sql
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jdk folder structure, sample folder Ulrich Scholz Java 4 01-26-2012 01:49 PM
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Graph Data structure to storing components in spice netlist.. Jonny C++ 2 05-02-2006 08:59 AM
Storing the size of an array in the structure itself Wynand Winterbach C Programming 22 07-26-2004 05:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57