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

Hey guys, For this challenge, feedback is limited, so it's difficult to debug. With the below code, please tell me wha

Hey guys,

For this challenge, feedback is limited, so it's difficult to debug. With the below code, please tell me what my issue is with this code?

contents.py
import os


def dir_contains(a_path, file_name_list):
    # Hold return value
    b = False

    # Loop through files in dir
    for dirname, dirnames, dirfilenames in os.walk(a_path):

        # Counter
        count = 0

        # Loop through all dir filenames
        for dirfilename in dirfilenames:

            # Check if all files are in num_files
            if count == len(file_name_list):
                b = True;
                break;

            if dirfilename in file_name_list:
                count+=1
                continue

        return b;

2 Answers

You could always try your code in a workspace if you want better access to debugging. I changed the order of your if blocks so that you check the value of count after it has been incremented and removed continue so that the check executes:

import os

def dir_contains(a_path, file_name_list):
    # Hold return value
    b = False

    # Loop through files in dir
    for dirname, dirnames, dirfilenames in os.walk(a_path):

        # Counter
        count = 0

        # Loop through all dir filenames
        for dirfilename in dirfilenames:

            # Check if all files are in num_files
            if dirfilename in file_name_list:
                count+=1

            if count == len(file_name_list):
                b = True;
                break; 

        return b; 

Try a test case of 1 file in file_name_list to see why this should be so.

Yes, of course: workspaces! Thanks