By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 2,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

directory listing

P: n/a
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


Nov 11 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
"SU News Server" <mk*****@syr.edu> wrote:
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>

Nov 11 '05 #2

P: n/a
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)
--
Richard
Nov 11 '05 #3

P: n/a
"Fredrik Lundh" <fr*****@pythonware.com> wrote:
"SU News Server" <mk*****@syr.edu> wrote:
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.

or use the glob module instead:

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

hope this helps!

</F>


Nov 11 '05 #4

P: n/a
Richard Townsend <ri*****@nospam.com> wrote:
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
Nov 11 '05 #5

P: n/a
On 11 Nov 2005 22:00:04 GMT, Michael Konrad wrote:
Richard Townsend <ri*****@nospam.com> wrote:
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


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?
--
Richard
Nov 11 '05 #6

P: n/a
Michael Konrad wrote:
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")
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? :)


use two variables:

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

</F>

Nov 11 '05 #7

P: n/a
On 11 Nov 2005 22:00:04 GMT, Michael Konrad <mk*****@syr.edu> wrote:
Richard Townsend <ri*****@nospam.com> wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list

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
Nov 11 '05 #8

P: n/a
Shi Mu wrote:
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>

Nov 11 '05 #9

P: n/a
On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote:
Shi Mu wrote:
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!
Nov 11 '05 #10

P: n/a
"Shi Mu" wrote:
but i am curious why the line of "print x" does not show
anything.


because your c:\temp directory is empty ?

</F>

Nov 11 '05 #11

P: n/a
On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote:
"Shi Mu" wrote:
but i am curious why the line of "print x" does not show
anything.


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?
Nov 11 '05 #12

P: n/a
"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>

Nov 11 '05 #13

P: n/a
Shi Mu wrote:
On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote:
Shi Mu wrote:
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
Nov 22 '05 #14

P: n/a

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>

Nov 22 '05 #15

P: n/a

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>

Nov 22 '05 #16

P: n/a

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>


Nov 22 '05 #17

P: n/a
Sorry about that, I guess send was working.

Michael Konrad wrote:

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>

Nov 22 '05 #18

P: n/a
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

Nov 22 '05 #19

P: n/a
thanks a lot!

On 11/11/05, Michael Konrad <mk*****@syr.edu> wrote:

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>


--
http://mail.python.org/mailman/listinfo/python-list

Nov 22 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.