directory listing

jay.dow at gmail.com jay.dow at gmail.com
Sat Nov 12 14:50:03 EST 2005


Shi Mu:

Before all you were doing was defining a function with:
import os

def buildList( directory='c:\TEMP' ):
    dirs = [ ]
    listing = os.listdir(directory)
    for x in listing:
        x = os.path.join(directory, x)
        print x
        if os.path.isdir(x):
            dirs.append(x)
    return dirs

when you python this file, it does not execute the function, it only
defines it.
Later Lundh told you to add:
print buildList()

to the end of the file.  Not only does this execute buildList() but it
also prints out the list "dirs" that buildList returns.  So the first
time it wasn't that "print x" wasn't printing anything, it was only
that you weren't executing the function buildList().  If, at the end of
the file, you put buildList()  you will only see output values
corresponding to the print x statement




More information about the Python-list mailing list