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

S M Tasfiqul Asif
S M Tasfiqul Asif
4,038 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
217,627 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.