FOR LOOPS

danielx danielwong at berkeley.edu
Mon Jul 31 23:15:09 EDT 2006


OriginalBrownster wrote:
> I am using a class called UploadedFile.
> I want to create a for loop to itterate through the objects within file
> name
>
> class UploadedFile(SQLObject):
> 	filename = StringCol(alternateID=True)
> 	abspath = StringCol()
> 	uniqueid = IntCol()
>
> I'll show you a snippit of the code I am trying to use it in::
>
>
>         zip= ["zip.txt"]
>         file_path = [myfile.filename for myfile in
> UploadedFile.select(orderBy=UploadedFile.q.filename)]
>
>         if kw:
>             for filename in file_path:
>                 zip.append(filename)
>             flash('Options selected'+ str(kw) + str(zip))
>         else:
>             pass
>
> When i run this the flash displays all the values for kw...however zip
> only shows up as "zip.txt" using the str function. Meaning that the FOR
> LOOP is not working correctly.
>
> Any ideas why this is happening?? perhaps someone could shoe me how to
> make a proper list for this if this is incorrect.
>
> Thank you

I'm not really familiar with SQLObject, but are you sure file_path is
not empty?

Also, a few small comments:

1. zip is a builtin function. I would choose a different name, even
though this is not strictly necessary.

2. You can accomplish what you seem to be intending with your loop
using list.extend.

3. That's an interesting thing to put in an else case. Why did you do
it that way instead of just leaving it out?




More information about the Python-list mailing list