Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python for File Systems Navigation Dir Contents

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Trying to master Python

I'll be the first to admit that Python is not my strongest language, but I am trying to change that. It seems that "baby steps" are the way for today, however.

I feel my code should be doing what the challenge asks, but I get the error:

Got 'False' for some existing paths.

Any hints? I'm really trying to get along with Python. :)

contents.py
import os

def dir_contains(path, names):
    for name in names:
        if os.path.exists(name):
            return True
        else:
            return False

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Jason,

The key with this one is that the challenge is asking you to only return True if all of the files from the list are in the directory. If even one file is non-existent, you return False.

In your code, you are returning a boolean in the first iteration of the loop, so the function won't ever get a chance to check anymore file names.

Also, when using os.path.exists(), you will need the full file path to the file, not just the name itself.

Hope this helps.