os.path.isdir question

yoz yoz at home.havin.us
Sat Mar 15 22:27:46 EDT 2008


lampshade wrote:
> Hello,
> 
> I'm having some problems with  os.path.isdir  I think it is something
> simple that I'm overlooking.
> 
> #!/usr/bin/python
> import os
> 
> my_path = os.path.expanduser("~/pictures/")
> print my_path
> results = os.listdir(my_path)
> for a_result in results:
>     if os.path.isdir(str(my_path) + str(a_result)):
>         results.remove(a_result)
> 
> for x in results: print x
> 
> The problem is, that the directories are never removed.  Can anyone
> point out what I'm missing that is causing the bug?  Is there a better
> way of doing this?
> 
> Thanks,

Your code works fine for me ..

Someone will probably complain that this isn't pythonic or hopefully 
come up with an even neater way but what about this:

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
files=os.walk(my_path).next()[2]
for x in files: print x

EdH.



More information about the Python-list mailing list