Traverse .blend file to get list of collections

Hey, I’m walking through a directory to find a blend file, but I need to get all the collections it contains. Since it’s possible to go into the Collections folder it must be possible to list the contents

import bpy
import os

assetDir = "H:/Shared drives/Proj/2_PRO/Assets/Props/"
section = "\\Collection\\"
load = "Bench"

for root, dirs, files in os.walk(assetDir):
    path = root.split(os.sep)
    for file in files:
        if file.endswith(".blend"):
            path = root + "/" +file 
            directory = path + section
            filename  = load
            
            try:
                bpy.ops.wm.link( 
                    filename=filename,
                    directory=directory)
            except:
                pass

So this will check if the collection exists and link it in, but how can I list the contents.
Doing “os.listdir(directory)” will return a fileNotFound error.

It’s important to list the collections without linking them into the scene first.

this is unsurprising because the contents of a blend file are not part of your filesystem. you have to open the file to get to its contents. once you’ve loaded it, it’s trivial to examine the collections using the data api.

Ah, it turns out that loading datablocks is temporary and they get removed upon reloading the file, so it doesn’t strain the system as much as I thought it would. Thanks for putting me on the right track