Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python for File Systems Navigation Dir Contents

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,624 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,275 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.