kmore
kmore

Reputation: 13

How do I recursively find a specific subfolder name in a directory using Python / Regular expressions?

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

Upvotes: 0

Views: 5099

Answers (1)

biobirdman
biobirdman

Reputation: 4120

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.

Upvotes: 2

Related Questions