473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

directory listing

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
19 3278
"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
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
"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
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
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
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
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
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
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
"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
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
"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
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

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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dean | last post by:
Hi I've got a question relating to using Javascript on an Intranet. I have a directory with a list of files in the format week36.xls, week37.xls and I want to write a script that will scan...
2
by: Tom | last post by:
I need to get a directory listing through http. If I put the directory path in the browser address bar such as http://somewebpage.com/subdir I get the listing of the directory. Of course this is...
8
by: gil | last post by:
Is it possible to prevent a browser from listing the entire contents of a folder? The site, is hosted on my ISP with the following layout- site/ "user name from ISP" pagefile (dir)...
2
by: ngr | last post by:
I used to use the DirListox control in VB6 but this is no longer supported in VB.NET. What I want to do: I want to take a directory listing and show any subdirectories in a list. When you click...
8
by: dougawells | last post by:
Hi - I'm hoping for help with the auto-generation of a hyperlinked listing of all files in a directory. The server I use does not auto-generate this. So, when someone comes to this directory and...
7
by: epikto | last post by:
I have a mapped share that I am trying to get a listing of all the files that it contains. I use the following code to access the contents String files = Directory.GetFiles(path); I can then...
4
by: techusky | last post by:
I have a *very* simple script written that displays the directory listing of the current working directory, but I am having some difficulty when I try to change folders. Basically, I have my $dir...
2
by: marcusadeleon | last post by:
Hi, I was wondering how to open up a web directory to get a file listing. The directory I want to open allows directory listings, but has a directory password. Meaning it gives me a prompt to...
1
by: Steve | last post by:
My site is hosted on a Godaddy reseller site. Godaddy only allows the use of Curl to access remote sites. What is the method for listing a directory after connecting to the site with Curl? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.