You get this error because you are trying to open name , this is just the file name, not the full relative path. What you need to do is open(os.path.join(root, name), 'r') (I added mode, as this is good practice).
for root, dirs, files in os.walk(path): for name in files: if name.endswith('.sql'): filepath = os.path.join(root, name) if 'gen_dts' in open(filepath, 'r').read(): print filepath
os.walk() returns a generator that gives you tuples of the type (root, dirs, files) , where root is the current directory, and dirs and files are the names of directories and files, respectively, that are in the root directory. Note that these are names, not paths; or, to be precise, this is the path to this directory / file relative to the current root directory, which is another way of saying the same thing. Another way to think about directories and files in dirs and files will never have a slash in them.
One last moment; Directory root paths always start with the path you pass to os.walk() , regardless of whether it was relative to your current working directory or not. So, for os.walk('three') root in the first tuple will be 'three' (for os.walk('three/') it will be 'three/' ). For os.walk('../two/three') it will be '../two/three' . For os.walk('/one/two/three/') it will be '/one/two/three/' ; the second may be '/one/two/three/four' .
source share