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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,589 Points

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