473,663 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ 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 4093
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(pa th+'/'+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********@gma il.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(pa th+'/'+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************ *************** ************@py thon.org>, Amit Khemka
wrote:
On 4/5/07, Amit Khemka <kh********@gma il.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(pa th+'/'+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(pa th+'/'+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
2691
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 c:\inetpub\wwwroot like c:\inetpub\wwwroot\myfldr and use http://localhost/myfldr/myASP.asp
7
3481
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 size and there will be 12 millions of them (at the beginning)... I expect a 8% growth every year. We will also have to perform some cleanup jobs to delete images that are not longer referenced by the master table. We'll also have to...
4
2723
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 my items and I can review things at anytime - on the desktop where I created all this - no problem so far. I networked the mdb file (only) across to my laptop and found the pictures don't show. So I guess my database table is only storing links to
4
9891
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 string representing radio button values. Thanks, -hazz
4
4632
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 webserver. These images can then be shown on to webpages throughout the site. What would be the best way of storing these images? Just by saving them in a folder on de webserver or in a database?
6
3171
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 entries with the entry in the other table? Having a separate table would result in two queries instead of one, but you wouldn't have to deal with the overhead of serializing and unserializing data. -- Kyle
3
2250
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 throught SourceSafe explorer or Visual Studio will attempt to open the web project from wwwroot, not your work directory." If I understand this correctly (by no means a certainty) it's saying
6
3191
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 me like there were performance issues at the time. How about the different types? The MS docs I would expect Access to differentiate and handle appropriately (i.e. .DOC and .XLS).. but how about ..PDF? and can I stash a .TXT document in the...
0
2353
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 handy, easy to backup, and modularized. My setup has not changed much over the years. As of now I have: C:\apache2.2 C:\mysql C:\Php
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4181
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
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 we have to send another system
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.