2.49b, Get list of files/.blend files in directory?

What I want to do is go through a directory and find all the blend files. Then go through each of those .blends and if ‘group x’ exists I want to link it, then add an instance of that group to the scene.

I’m stuck on the first part, I can’t find a method to get a list of files in a directory. I can imagine the rest is straight forward after I study the api more and fiddle around with it.

I know GameLogic has a getBlendFileList, but cannot find similar for regular blender.

Any ideas? thanks alot!

hi Rhys S,
you will find the code in bundled scripts, e.g. import_dxf.py (def multi_import(DIR): or final “DEBUG ONLY”)
There you have to replace:
EXT = ‘.dxf’ with EXT = ‘.blend’

Recursive one using os.walk():

import os

def locate(substr, root=os.getcwd()):
    for path, dirs, files in os.walk(root):
        for filename in files:
            if substr in filename:
                yield os.path.abspath(os.path.join(path, filename))

Given a name pattern:

import os
import re

def locate(pattern, root=os.getcwd()):
    regex = re.compile(pattern)
    for path, dirs, files in os.walk(root):
        for filename in files:
            if regex.match(filename):
                yield os.path.abspath(os.path.join(path, filename))

hi log0, indeed an universal solution, thank you.

I was looking through the blender docs so much for this sort of module I forgot about the python ones.

I only require to go through one directory so I think listdir() should suffice.

import Blender
import os

def listFiles(dir, ext):
    fileList = []
    for file in os.listdir(currentDir):
        if file[-len(ext):] == ext:
    return fileList
currentDir =  Blender.sys.expandpath('//')
print listFiles(dir = currentDir, ext = '.blend')

Thanks all for the nice help.