import globimport os
def _getDirs(base):return [x for x in glob.iglob(os.path.join( base, '*')) if os.path.isdir(x) ]
def rglob(base, pattern):list = []list.extend(glob.glob(os.path.join(base,pattern)))dirs = _getDirs(base)if len(dirs):for d in dirs:list.extend(rglob(os.path.join(base,d), pattern))return list
import fnmatch, os
def filepaths(directory, pattern):for root, dirs, files in os.walk(directory):for basename in files:try:matched = pattern.match(basename)except AttributeError:matched = fnmatch.fnmatch(basename, pattern)if matched:yield os.path.join(root, basename)
# usageif __name__ == '__main__':from pprint import pprint as ppimport repath = r'/Users/hipertracker/app/myapp'pp([x for x in filepaths(path, re.compile(r'.*\.py$'))])pp([x for x in filepaths(path, '*.py')])
import os, glob
def _globrec(path, *exts):""" Glob recursively a directory and all subdirectories for multiple file extensionsNote: Glob is case-insensitive, i. e. for '\*.jpg' you will get files endingwith .jpg and .JPG
Parameters----------path : strA directory nameexts : tupleFile extensions to glob for
Returns-------files : listlist of files matching extensions in exts in path and subfolders
"""dirs = [a[0] for a in os.walk(path)]f_filter = [d+e for d in dirs for e in exts]return [f for files in [glob.iglob(files) for files in f_filter] for f in files]
my_pictures = _globrec(r'C:\Temp', '\*.jpg','\*.bmp','\*.png','\*.gif')for f in my_pictures:print f
import os, glob, itertools
results = itertools.chain.from_iterable(glob.iglob(os.path.join(root,'*.c'))for root, dirs, files in os.walk('src'))
for f in results: print(f)
import glob
#file_names_array = glob.glob('path/*.c', recursive=True)#above works for files directly at path/ as guided by NeStack
#updated versionfile_names_array = glob.glob('path/**/*.c', recursive=True)
你可能需要更多
for full_path_in_src in file_names_array:print (full_path_in_src ) # be like 'abc/xyz.c'#Full system path of this would be like => 'path till src/abc/xyz.c'
def flipProperties(searchDir):print "Flipping properties to point to live DB"for root, dirnames, filenames in os.walk(searchDir):for filename in fnmatch.filter(filenames, 'live-*.*'):targetFileName = os.path.join(root, filename.split("live-")[1])print "File "+ os.path.join(root, filename) + "will be moved to " + targetFileNameshutil.move(os.path.join(root, filename), targetFileName)