directory listing

Fredrik Lundh fredrik at pythonware.com
Fri Nov 11 18:08:40 EST 2005


"Shi Mu" wrote:

> print buildList() gets lots of stuffs from my temp directory(there do
> exist lots of files).
> But why "print x' has nothing?

C:\>more script.py
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

print buildList()

C:\>dir temp
...

2005-11-12  00:00       <KAT>          .
2005-11-12  00:00       <KAT>          ..
2005-11-12  00:00                   20 bacon.dat
2005-11-12  00:00       <KAT>          egg
2005-11-12  00:00                   20 spam.txt
               2 fil(er)                  40 byte
               3 katalog(er)   9 818 021 888 byte ledigt

C:\>python script.py
c:\TEMP\bacon.dat
c:\TEMP\egg
c:\TEMP\spam.txt
['c:\\TEMP\\egg']

</F>






More information about the Python-list mailing list