[Tutor] mysql store directory information

Tiago Saboga tiagosaboga at terra.com.br
Fri Apr 7 03:54:01 CEST 2006

Em Qui 06 Abr 2006 15:41, Danny Yoo escreveu:
> On Thu, 6 Apr 2006, Tiago Saboga wrote:
> > As a first part of a project, I need to store a directory tree in a
> > mysql db.  I'm wondering if there is a canonical way of doing that. I
> > don't know if it's an appropriate question for this list, but I think
> > it's not only a choice of db design, but also a choice of appropriate
> > python tools.
> Hi Tiago,
> What are the interesting features of a directory?  You might want to first
> model what you want, and then figure out an appropriate database table
> structure to represent that model.

Hi, and thank you.

I knew I had to explain longer what I wanted, but I was kind of frustrated by 
the difficulty of writing in english. But I'll try to think less about it, 
and go further.

As I said, this is part of a project. For each of these files, I'll have a 
database of informations, and this is what really matters, and I have a data 
model for that. But I want also to able to find these files, which are on 
removable media (cds and dvds). As I'll have to store where's the file, I 
thought I could as well store some other basic infos, at least the size (I 
really don't know yet what else could be useful later).

> > My first approach is a table with the following columns:
> > id - path - file name - size
> So maybe we can say that a Directory can be modeled as:
> ######
> class Directory:
>     def __init__(self, id, path, file_name, size):
>         self.id = id
>         self.path = path
>         self.file_name = file_name
>         self.size = size
> ######
> But why will you want to store this structure in the database, if it's
> already available on disk?  Why not query the directory directly?

See above.

> > I'm starting to code it, and I'd like to know if you have a better
> > suggestion...
> I'd flesh out a few more of the requirements first; the requirement to
> store the directory in the database is slightly vague, so you probably
> will want to ask more questions about what the problem's really about.
> You might find something like SQLObject useful:
>     http://www.sqlobject.org/
>     http://www.turbogears.org/about/sqlobject.html
> where you go fairly directly from data model to SQL table structure with
> SQLObject providing the default mapping strategy.

Hey, this is *really* great ;-)

Hey, I love it.

OK, but now why would I use such a directory class as you proposed above? 
(preliminar question: isn't it rather a file class, as it has only one 
filename? Anyway, I see your point.) I would make a SQLobject class for 
files, and feed it with something like 

def get_dirtree(path):
  tree = []
  for dirpath, dirnames, filenames in os.walk(path):
    if filenames:
      for file in filenames:
        size = os.path.getsize(os.path.join(dirpath,file))
  return tree

What do you think? Of course, it could be the __init__ function of class, but 
I don't see why.

Thanks, again.


More information about the Tutor mailing list