Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > directory listing

Reply
Thread Tools

directory listing

 
 
SU News Server
Guest
Posts: n/a
 
      11-11-2005
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




 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-11-2005
"SU News Server" <(E-Mail Removed)> 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>



 
Reply With Quote
 
 
 
 
Richard Townsend
Guest
Posts: n/a
 
      11-11-2005
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
 
Reply With Quote
 
Michael Konrad
Guest
Posts: n/a
 
      11-11-2005
"Fredrik Lundh" <(E-Mail Removed)> wrote:

> "SU News Server" <(E-Mail Removed)> 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>
>
>
>
>


 
Reply With Quote
 
Michael Konrad
Guest
Posts: n/a
 
      11-11-2005
Richard Townsend <(E-Mail Removed)> 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


 
Reply With Quote
 
Richard Townsend
Guest
Posts: n/a
 
      11-11-2005
On 11 Nov 2005 22:00:04 GMT, Michael Konrad wrote:

> Richard Townsend <(E-Mail Removed)> 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-11-2005
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>



 
Reply With Quote
 
Shi Mu
Guest
Posts: n/a
 
      11-11-2005
On 11 Nov 2005 22:00:04 GMT, Michael Konrad <(E-Mail Removed)> wrote:
> Richard Townsend <(E-Mail Removed)> 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-11-2005
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>



 
Reply With Quote
 
Shi Mu
Guest
Posts: n/a
 
      11-11-2005
On 11/11/05, Fredrik Lundh <(E-Mail Removed)> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
directory listing details =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 2 05-24-2005 05:48 PM
Asking for directory listing on AP 350 flash: atcat Cisco 2 04-30-2005 08:27 AM
Directory listing Todd Lu ASP .Net 2 08-31-2004 03:26 PM
Directory Listing JD Perl 0 08-23-2003 11:53 PM



Advertisments