os.path.isdir is always returning false

Just started with learning python and tried something, most people here would call more than simple.

I just wanted to list all directories within my main directory. So I user the following code:

#!/usr/bin/env python

import os

if os.path.isdir("/home/testaccaunt/public_html"):
     print "I am a directory"
else:
     print "I am NO directory"

So that is just a first test for me and the result is always that my public_html folder is no directory. I also tested it with os.path.isfile and os.path.islink and it alway tells me that it is something different.

Hopefully you have an idea on that. It is on CentOs 5.

it shouldn't be. os.path.isdir() is supposed to do what it says it does. show your a listing of "/home/testaccaunt" directory.

As a found it at nearly every beginner guide, with the os.listdir option I am listing all files and folders within my directory and the os.path.isdir is used to find out that one is a file or a directory.

So I just the tried the following piece of code:

#!/usr/bin/env python

import os

filelist = os.listdir("/home")

for file in filelist:
     if os.path.isdir(file):
          print "I am a directory"
      else:
          print "I am NO directory"

If I just remove the if, I am able to print all files and directories, but that tiny if clause is just not working. Any idea where my fault is?

try this

#!/usr/bin/env python
import os
root="/home"
for files in os.listdir(root):    
    if os.path.isdir(os.path.join(root,files)):
        print "Directory : ",files

Thanks for the fast reply, that works, but where is the big difference ?

Is there any reason why my posted code is just always telling that there is no directory?

to use os.listdir(), i always change directory first to the directory of interest

os.chdir("/home")
for files in os.listdir("."):
  .....

otherwise, use the full path by joining, as in the previous example.

Okay, now I got it. Thanks for all you help, I will now go ahead with my guides.

Just one question, could you recommend a good python editor?

see here. Its all about preferences.

Thanks again for your help.