Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > stupid question about os.listdir

Reply
Thread Tools

stupid question about os.listdir

 
 
Jason Kratz
Guest
Posts: n/a
 
      06-27-2003
Peter Hansen wrote:
> Jason Kratz wrote:
>
>>def getdirs(path):
>> os.chdir(path)
>> for entry in os.listdir(path):
>> if os.path.isdir(entry):
>> dirs.append(entry)
>>
>>if I run this from the command line on linux with 'python backup.py' it
>>works *if* I have os.chdir in there. if I comment it out it doesnt list
>>starting from the root dir...it starts in my home dir.

>
>
> This might mean you are not passing it an absolute path, but
> instead a relative one. Absolute paths (on Linux) are those
> which start with a / (forward slash). Anything without that
> will start from the current directory only.
>
> But without actual examples of which paths are failing, as
> Ben has asked for, we know nothing for certain.
>
> Is it also possible that you are not having a problem with listdir()
> at all, but with the values you are passing to os.path.isdir()?
> You realize that os.listdir() returns names that are *relative*
> to the path parameter you give it? So that if you then pass those
> to isdir() you will get nothing useful if you don't first do
> this instead? :
>
> entry = os.path.join(path, entry)
> if os.path.isdir(entry):
> dirs.append(entry)
>
> -Peter


Peter-

Thanks much for the reply. You are completely correct and I managed to
work thru it myself (just finished right before I read your posting) and
I actually had the exact code (aside from using a new var instead of
reusing entry) you have just above (with the join). Was going to post
it and find out if that was the "correct" way of doing it

This is my code:

import os
import os.path

def getdirs(path):
dirs=[]

for entry in os.listdir(path):
fullpath=os.path.join(path,entry)
if os.path.isdir(fullpath):
dirs.append(fullpath)
return dirs

print getdirs('/')

 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      06-27-2003
Jason Kratz wrote:

> aha! I found it! its the call to os.path.isdir. I'm not passing
> it
> a real pathname....just a string. I need to set my entries in my dir
> list as real pathnames (ie: with the slashes)...not just the text.
> question is how


A pathname _is_ just a string. Presumably the problem is that you're
getting the results of os.listdir, which are just the names of the
contents of the directory, not the relative paths to them:

max@oxygen:~/tmp% mkdir subdir
max@oxygen:~/tmp% touch subdir/a subdir/b subdir/c
max@oxygen:~/tmp% ls subdir
a b c
max@oxygen:~/tmp% python
Python 2.2.3 (#1, May 31 2003, 21:31:33)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.listdir('subdir')

['a', 'b', 'c']

So just join the paths:

>>> for f in os.listdir('subdir'):

.... p = os.path.join('subdir', f)
.... print p, os.path.isfile(p)
....
subdir/a 1
subdir/b 1
subdir/c 1

--
Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ ike anybody, I would like to live a long life.
\__/ Dr. Martin Luther King, Jr.
 
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
stupid, STUPID question! rincewind HTML 25 05-08-2009 01:07 PM
stupid question...waiting for a stupid answer Brandon McCombs Java 4 08-28-2006 06:57 PM
Stupid question. Please, only stupid responders. If you're not sureif you're stupid, you probably aren't. =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-18-2005 05:11 AM
stupid stupid stupid kpg MCSE 17 11-26-2004 02:59 PM
Stupid is as Stupid Does! Michael P Gabriel Digital Photography 3 06-26-2004 12:49 PM



Advertisments