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

S M Tasfiqul Asif
S M Tasfiqul Asif
11,514 Points

why my code is not working?

can someone please explain why my code is not working

import os

def dir_contains(path,file_list): dir_list = os.listdir(path) for file in file_list: if file in dir_list: return True else: return False

contents.py
import os

def dir_contains(path,file_list):
    dir_list = os.listdir(path)
    for file in file_list:
        if file in dir_list:
            return True
        else:
            return False  

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're on the right track, but you can't determine if you should return True while still in the loop. Only when the loop finishes will you know that every file was found.

But you can return False inside the loop if any file is not found.