Re: directory listing
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(dire ctory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(dire ctory)
#get just the directories
for x in listing:
if os.path.isdir(d irectory+os.sep +x):
dirs.append(x)
return dirs
Fredrik Lundh wrote:[color=blue]
> "Shi Mu" wrote:
>[color=green]
>> print buildList() gets lots of stuffs from my temp directory(there do
>> exist lots of files).
>> But why "print x' has nothing?[/color]
>
> C:\>more script.py
> import os
>
> def buildList( directory='c:\T EMP' ):
> dirs = [ ]
> listing = os.listdir(dire ctory)
> for x in listing:
> x = os.path.join(di rectory, 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.d at
> c:\TEMP\egg
> c:\TEMP\spam.tx t
> ['c:\\TEMP\\egg']
>
> </F>
>
>
>[/color]
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(dire ctory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(dire ctory)
#get just the directories
for x in listing:
if os.path.isdir(d irectory+os.sep +x):
dirs.append(x)
return dirs
Fredrik Lundh wrote:[color=blue]
> "Shi Mu" wrote:
>[color=green]
>> print buildList() gets lots of stuffs from my temp directory(there do
>> exist lots of files).
>> But why "print x' has nothing?[/color]
>
> C:\>more script.py
> import os
>
> def buildList( directory='c:\T EMP' ):
> dirs = [ ]
> listing = os.listdir(dire ctory)
> for x in listing:
> x = os.path.join(di rectory, 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.d at
> c:\TEMP\egg
> c:\TEMP\spam.tx t
> ['c:\\TEMP\\egg']
>
> </F>
>
>
>[/color]
Comment