0

New to Python and programming. I'm using a Mac Air w/ OS X Yosemite version 10.10.2.

I'm looking for a way to recursively find multiple subfolders, with the same name(e.g."Table"), without using a direct path(assuming I don't know what folders they might be in) and read the files within them using regular expressions or Python. Thank you in advance!

import sys
import os, re, sys
import codecs
import glob



files = glob.glob( '*/Table/.*Records.sql' )

with open( 'AllTables1.2.sql', 'w' ) as result:
    for file_ in files:
        print file_
        if len(file_) == 0: continue
        for line in open( file_, 'r' ):
            
            line = re.sub(r'[\[\]]', '', line)
            line = re.sub(r'--.*', '', line)
            line = re.sub('NVARCHAR', 'VARCHAR', line)
            line = re.sub(r'IDENTITY', 'AUTO_INCREMENT', line)
            line = re.sub(r'DEFAULT\D* +','', line)
            line = re.sub(r'\W(1, 1\W)', ' ', line)
        
        
            result.write( line.decode("utf-8-sig"))                 
            

result.close()
kmore
  • 13
  • 1
  • 3
  • And what happens when you run the code above? –  Feb 23 '15 at 17:51
  • I just get a blank document back. Thank you for the reply. – kmore Feb 23 '15 at 17:54
  • possible duplicate of [Use a Glob() to find files recursively in Python?](http://stackoverflow.com/questions/2186525/use-a-glob-to-find-files-recursively-in-python) – Darrick Herwehe Feb 23 '15 at 17:59
  • Well, start debugging...are you getting the right files from your `glob` call? –  Feb 23 '15 at 17:59
  • Thank you Darrick and Jack. I'll debug again and I already tried the suggested post. At best I end up finding all .sql documents in directory. – kmore Feb 23 '15 at 18:05

1 Answers1

2

You can use os.walk, which comes shippes with python for this purpose. As, the name suggest, os.walk will 'walk' thru your directory recursively and return the root, the dir and a list of file found in the dir.

http://www.tutorialspoint.com/python/os_walk.htm

You will find an example in the link that I gave above.

Hence for your example, you can achieve your goal you consider doing an os.walk, set up a regex to match folders with a given pattern and get the file with the matching name in your list of file.

For instanxe :

import os

for root, dir, filelist in os.walk('./'):
    if dir == 'results': # insert logic to find the folder you want 
        for file in filelist:
            if file  == 'xx': # logic to match file name 
                fullpath = os.path.join(root, file) # Get the full path to the file

the above example will find your wanted file names in a particular folder.

The Godfather
  • 4,235
  • 4
  • 39
  • 61
biobirdman
  • 4,060
  • 1
  • 17
  • 15
  • 1
    Note that `os.walk` returns a *list* of directories rather than a single directory, so you'd generally want to do something like `for root, dirs, filelist...: for dir in dirs: ...` – erwaman Sep 18 '18 at 15:57