directory listing

S

SU News Server

I've struggled with this for quite a while and I'm am just not sure
what is going on. I have the following code
import os

def buildList( directory='/Users/mkonrad' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory='.' or directory=os.getcwd()
Then it returns a list of directories.

Any ideas?

Thank you,
-Michael
 
F

Fredrik Lundh

SU News Server said:
I've struggled with this for quite a while and I'm am just not sure
what is going on. I have the following code
import os

def buildList( directory='/Users/mkonrad' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory='.' or directory=os.getcwd()
Then it returns a list of directories.

hint: if you're not sure what's going on in your program, adding
a print statement or two is an easy way to figure it out. like, say:

for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)

(try this before you continue)

:
:
:

the problem is that os.listdir returns a list of filenames, without
the preceeding directory name. you can add an os.path.join

for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
dirs.append(x)

or use the glob module instead:

listing = glob.glob(os.path.join(directory, "*"))

hope this helps!

</F>
 
R

Richard Townsend

On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:

Try passing the full pathname of each item to os.path.isdir()

You can create the pathname using os.path.join(directory, x)
 
M

Michael Konrad

Fredrik Lundh said:
SU News Server said:
I've struggled with this for quite a while and I'm am just not sure
what is going on. I have the following code
import os

def buildList( directory='/Users/mkonrad' )

dirs = [ ]

listing = os.listdir(directory)

for x in listing:
if os.path.isdir(x):
dirs.append(x)

return dirs

This always returns an empty list.
Now if I change it so that directory='.' or directory=os.getcwd()
Then it returns a list of directories.

hint: if you're not sure what's going on in your program, adding
a print statement or two is an easy way to figure it out. like, say:

for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)

Did that and I was just getting a bunch of [ ].
(try this before you continue)

:
:
:

the problem is that os.listdir returns a list of filenames, without
the preceeding directory name. you can add an os.path.join

for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
dirs.append(x)

OK. This works except each entry in dirs now includes the full path.
Is there an unjoin? :) I haven't spent any time trying to work this out.
 
M

Michael Konrad

Richard Townsend said:
On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:

Try passing the full pathname of each item to os.path.isdir()

You can create the pathname using os.path.join(directory, x)

I wonder if I can join ./, so I don't have the full path in each
entry?

Thank you for responding.
_Michael
 
R

Richard Townsend

I wonder if I can join ./, so I don't have the full path in each
entry?

Thank you for responding.
_Michael

Why not assign the os.path.join() result to a new variable, pass that to
os.path.isdir() but still append x to the list?
 
F

Fredrik Lundh

Michael said:
for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)

Did that and I was just getting a bunch of [ ].

if you printed "x" (the filename), that doesn't sound very likely.
maybe you printed some other variable? (like "dirs")
OK. This works except each entry in dirs now includes the full path.
Is there an unjoin? :)

use two variables:

for name in listing:
fullname = os.path.join(directory, name)
if os.path.isdir(fullname):
dirs.append(name)

</F>
 
S

Shi Mu

I wonder if I can join ./, so I don't have the full path in each
entry?

Thank you for responding.
_Michael
I tried this and no error reported but nothing appear on the console, why?

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
 
F

Fredrik Lundh

Shi said:
I tried this and no error reported but nothing appear on the console, why?

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

is that the entire script? you're defining a function, but you're
not calling it. try adding

print buildList()

at the end of the script.

</F>
 
S

Shi Mu

Shi said:
I tried this and no error reported but nothing appear on the console, why?

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

is that the entire script? you're defining a function, but you're
not calling it. try adding

print buildList()

at the end of the script.

It works but i am curious why the line of "print x" does not show
anything. many thanks!
 
S

Shi Mu

because your c:\temp directory is empty ?

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

Fredrik Lundh

Shi Mu said:
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>
 
P

Peter Hansen

Shi said:
Shi said:
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

It works but i am curious why the line of "print x" does not show
anything. many thanks!

Did you use directory='c:\TEMP' as shown above, or directory='c:\temp' ?
If you used the lower case version, you are not really checking the
temp directory, since \t represents a TAB character. If that's the
case, try using a forward slash instead: c:/temp .

-Peter
 
M

Michael Konrad

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 said:
Shi Mu said:
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>
 
M

Michael Konrad

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 said:
Shi Mu said:
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>
 
M

Michael Konrad

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 said:
Shi Mu said:
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>
 
M

Michael Konrad

Sorry about that, I guess send was working.

Michael said:
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 said:
Shi Mu said:
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>
 
J

jay.dow

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
 
S

Shi Mu

thanks a lot!

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 said:
Shi Mu said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top