When you use Python, you must use the correct modules to do what you want.
To find all files in a directory with extension shp, for example, there are much simpler solutions that was presented without the break, which is awful...(as the solution presented by Nathan W, but there are many, many others, just search on Internet)
Some examples with relevant modules:
1) with the glob module:
shapefiles only:
import glob
import os
os.chdir("mydir")
for files in glob.glob("*.shp"):
print files
shapefiles and geodatabases:
import glob
types = ('*.shp', '*.gbd') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files)) #files_grabbed = the list of shp and gbd files
if you want to search also in the subdirectories:
import glob
for f in glob.iglob("/mydir/*/*.shp"): #search immediate subdirectories
print f
2) with os.listdir and list comprehension (in two lines) -> list of results
path = 'mydir'
shape_files = [f for f in os.listdir(path) if f.endswith('.shp')]
gdb_files = [f for f in os.listdir(path) if f.endswith('.gdb')]
3) with fnmatch module:
import fnmatch
for file in os.listdir('path'):
if fnmatch.fnmatch(file, '*.shp'):
print file
and many others solutions, recursive etc.