Eric Walker wrote:
> New to Python and trying to do some class stuff with a simple task. 
> Problem:
> 1) get a list of file names in a directory
> 2) create variables with the same name of each filename pulled from the 
> directory.
> 3) Create an object for each and pass into the __init__ method the stringname 
> of the file name.
> 
> This way I get a collection of objects that are the same name as the file 
> name 
> and within each instance of the class , a particular attribute will have the 
> string name of the object.  Hope this isn't too confusing.. example.

What will you do with the names and objects once you have them? A better 
approach is probably to keep a dictionary that maps names to objects. If your 
object is really just storing the name you might as well just keep a list of 
names - the object isn't adding any value. If the object is going to have more 
behaviour then use a dict. If you really just want to print the names then you 
don't need to store them at all. For example with a dict:

class TPROJ:
    # as before
        
def getNames():
    import os
    currentDir=os.getcwd()
    temp=currentDir + '/TEMP'
    os.chdir(temp)
    baseList=os.listdir(".")
    nameDict = {}
    for name in baseList:
        nameDict[name] = TPROJ(name)
        print name
    return nameDict

HTH,
Kent
> 
> 
> class TPROJ:
>     def __init__(self,value):#createMethod auto executes since it has __
>         self.BASENAME = value
>         
>    def display(self):#display method
>         print self.BASENAME
>         
> def getNames():
>     import os
>     currentDir=os.getcwd()
>     temp=currentDir + '/TEMP'
>     os.chdir(temp)
>     baseList=os.listdir(".")
>     for name in baseList:
>         name = TPROJ(name)
>         print name
> 
> Can anyone see what I am trying to do?
> 
> Python Newbie.......
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to