Searching for files with os.walk if no files are found matching the search criteria- Python
Question: Right now I am using os.walk to recursively search directories and
subdirectories for any autorun.inf file. I know I'm not doing it in the best
way so if you have a better way for that too, please let me know. If it finds
anything matching autorun it will output the file's contents for each file
found matching the criteria. However when I try to make an else statement for
if no files were found that matched, it prints "No file matching the criteria
found" for each file that doesn't match instead of only one time if that makes
sense. I only want it to say it once if no file matching the criteria is
found. Here's what I have so far.

    
    
    import os
    from os.path import join
    for (dirname, dirs, files) in os.walk('/home/user/Documents/mystuff'):
        for filename in files:
        if filename.startswith('autorun'):
            thefile = os.path.join(dirname,filename)
            f = open(thefile)
            print f.read()
        else:
            print "No file matched the criteria"
    


Answer: 
    found = False
    
    for (dirname, dirs, files) in os.walk('/home/user/Documents/mystuff'):
        for filename in files:
            if filename.startswith('autorun'):
                thefile = os.path.join(dirname,filename)
                f = open(thefile)
                print f.read()
                found = True
    
    if not found:
        print "No file matched the criteria"
    

