Iterate over folders in Python and files containing strings

I am very new to python. I need to iterate through subdirectories of this directory and return all files containing a specific line.

for root, dirs, files in os.walk(path): for name in files: if name.endswith((".sql")): if 'gen_dts' in open(name).read(): print name 

It was the closest I was.

The syntax error I get is

 Traceback (most recent call last): File "<pyshell#77>", line 4, in <module> if 'gen_dts' in open(name).read(): IOError: [Errno 2] No such file or directory: 'dq_offer_desc_bad_pkey_vw.sql' 

The file 'dq_offer_desc_bad_pkey_vw.sql' does not contain 'gen_dts' in it.

I appreciate the help in advance.

+5
source share
2 answers

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' .

+8
source

Files are simply file names. Before you open them, you must add a path to it. Use os.path.join.

0
source

All Articles