473,466 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
4 4064
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Barry G. Sumpter | last post by:
Complete newbie here. Just sorted out that myASP.asp will ONLY execute on c:\inetput\wwwroot when I access it thru http://localhost/myASP.asp but if I copy myASP.asp to a sub folder of...
7
by: Benoit St-Jean | last post by:
I am looking at options/ways to store 12 million gif/jpg images in a database. Either we store a link to the file or we store the image itself in the database. Images will range from 4k to 35k in...
4
by: Rednelle | last post by:
Greetings all, As a newbie, using Access 2000, I would appreciate advice on the best way to include pictures. I have developed a 'Home Inventory' database which can include jpeg thumbnails of...
4
by: Hazzard | last post by:
What is the best way to do this? Binary with 0 representing off and 1 on? Int16 with 1 representing first button, 2 the second, 3 ... varchar with a character values at certain positions in the...
4
by: IkBenHet | last post by:
Hello, I am working on an ASP.NET based website project. Without going into much detail; One of the function on this website will be the possibility to upload images via a ASP.NET form to the...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
3
by: teddysnips | last post by:
I've been brought onto a new project and there's a file that lays out the preferred method of obtaining the source code. This file starts as follows: "Do not get the source for the web project...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
0
by: Chuck Anderson | last post by:
I'm trying to come with a good organization plan for the Apache, Php, MySQL development software on my home PC and am wondering what sort of folder structure others may have come up with that seems...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.