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()

5
  • And what happens when you run the code above? Commented Feb 23, 2015 at 17:51
  • I just get a blank document back. Thank you for the reply. Commented Feb 23, 2015 at 17:54
  • possible duplicate of Use a Glob() to find files recursively in Python? Commented Feb 23, 2015 at 17:59
  • Well, start debugging...are you getting the right files from your glob call? Commented Feb 23, 2015 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. Commented Feb 23, 2015 at 18:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.