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

Jonathan Beard
Jonathan Beard
1,681 Points

Need assistance with dir_contains function

I have been working on this challenge without any luck. It is asking to verify the existence of files in a list. I think this does that but I'm sure I'm missing something.

The error I get is some returned False. I wish you could debug on the console to get better information.

def dir_contains(path, file_list):
    for files in file_list:
        if os.path.isfile(files):
            return True
        else:
            return False

5 Answers

Steven Parker
Steven Parker
218,583 Points

You seem to have a good idea there, and you're pretty close. Here's a few hints:

  • you must join the path with each file name to be able to check it
  • you must check every file before you can return "True"
  • you can return "False" as soon as one file doesn't check out
Jonathan Beard
Jonathan Beard
1,681 Points

I'm just stuck on the checking all before returning True....

def dir_contains(path, file_names):
    for files in file_names:
        file = os.path.join(path, files)
        if os.path.isfile(file):
            return True
        else:
            return False
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,063 Points

You have the right idea, but there are two issues:

  • you are returning in the first iteration whether it's True of False. Instead, only return when you known one is False or all are True.
  • the argument to isfile needs to be a full path. Use is.path.join to create full path from function parameters.

Post back if you need more help. Good luck!!!

Jonathan Beard
Jonathan Beard
1,681 Points

This seems to have worked. However, is this the best way to get the correct answer?

def dir_contains(path, file_names):

    true_files = 0
    false_files = 0

    for files in file_names:
        file = os.path.join(path, files)
        if os.path.isfile(file):
            true_files += 1
        else:
            false_files += 1
    if true_files == len(file_names):
        return True
    else:
        return False
Steven Parker
Steven Parker
218,583 Points

You didn't really need to count the files, you can return "False" the first time one isn't found, and then return "True" after the loop has finished.

Jonathan Beard
Jonathan Beard
1,681 Points

I had some one help me here at work.

def dir_contains(path, file_names):

    for files in file_names:
        file = os.path.join(path, files)
        if not os.path.isfile(file):
            return False
    return True
Steven Parker
Steven Parker
218,583 Points

With a handy private mentor, you may not need the forum! But that's exactly what I had in mind when I gave those hints originally.