Veeeeery Basic Question

Fredrik Lundh effbot at
Sun Feb 27 18:19:29 EST 2000

Eric Stewart <ericstewart at> wrote:
> I guess this will be a CGI script that will run in the Directory
> cgi-bin, which will be referenced by the HTML.

sure.  but if this is really your first script,
you better play with it a while before you
try to run it under the web server...

> The image files which i will select from will be in a seperate
> directory, i.e C:Web Stuff\Images
> Here is the problem I'm having.  (Understand that I've NEVER written
> anything before).  How do I iterate over the image files in the
> directory?  I thought that a for loop would work.  sort of...
> import random
> for myFiles in someDirectory:
>    myImage=random.choice(Myfiles)
>    return myImage

here's a start:


import os
import random

# get a list of files
myFiles = os.listdir("c:/Web Stuff/Images")

# pick a random file from that list
print random.choice(myFiles)


hope this helps (a little bit, at least)


More information about the Python-list mailing list