directory listing
Michael Konrad
mkonrad at syr.edu
Fri Nov 11 23:44:24 EST 2005
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(directory)
#get just the directories
for x in listing:
if os.path.isdir(directory+os.sep+x):
dirs.append(x)
return dirs
Fredrik Lundh wrote:
> "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