newbie class troubles

Calvelo Daniel dcalvelo at pharion.univ-lille2.fr
Sun Sep 17 20:13:21 CEST 2000


Brett Lempereur <a.lempereur[remove this|@|remove this]cableinet.co.uk> wrote:
: class filesearch:
:     # Any public variables i need
:     exceptions = []
:     filelist = []
:     dirlist = []
:
:     def search(self, top):
:         try:
:             tmpList = os.listdir(top)
:         except os.error:
:             return
:
:         for item in tmpList:
:             if item not in exceptions:
:                 name = os.path.join(top, name)
:                 if os.path.isdir(name):
:                     self.dirlist.append(name)
:                     search(self, name)
:                 else:
:                     self.filelist.append(name)

: that's the class.  you already know the error

[It's a "method must be called with instance first argument, for the record]

<speculation reason="not enough info on the error">

I gather you are not creating an instance of your class and using its 
"search" method, but rather calling 'filesearch.search(some_dir)' or
something.

</speculation>

Let me go through your code:

: class filesearch:

You are using a class for your prcedure. That's your decision, so I won't
change (I wouldn't have done it that way).

:     # Any public variables i need
:     exceptions = []
:     filelist = []
:     dirlist = []

I suppose these should be instance variables, so you should declare them
into an '__init__()' method.

:     def search(self, top):
:         try:
:             tmpList = os.listdir(top)
:         except os.error:
:             return
:
:         for item in tmpList:
:             if item not in exceptions:
:                 name = os.path.join(top, name)

This gives a NameError; I suppose that you mean 'item' by 'name'

:                 if os.path.isdir(name):
:                     self.dirlist.append(name)
:                     search(self, name)

This is weird a method call; at this point it should also raise a NameError,
for scoping reasons.

:                 else:
:                     self.filelist.append(name)

I changed your source to:

import os
class filesearch:
    # Any public variables i need
    def __init__(self):
        self.exceptions = []
        self.filelist = []
        self.dirlist = []

    def search(self, top):
        try:
            tmpList = os.listdir(top)
        except os.error:
            return

        for item in tmpList:
            if item not in self.exceptions:
                name = os.path.join(top, item)
                if os.path.isdir(name):
                    self.dirlist.append(name)
                    self.search(name)
                else:
                    self.filelist.append(name)

And that works like this (if you include a full traceback with the code that
generated it, I'm pretty sure we could be much more helpful around here):

>>> a = filesearch() # create an instance of the filesearcher
>>> a.search('.') # update the lists
>>> a.filelist # show it

Does that solve your problem, or does it solve another problem I shouldn't be
fiddling with ona Sunday afternoon?


There-are-no-stupid-questions-only-stupid-answers-ly y'rs, 

Daniel.


-- Daniel Calvelo Aros
     calvelo at lifl.fr



More information about the Python-list mailing list