pythonsubdirectorytrimesh

Walking through directory path and opening them with trimesh


I have the following code:

import os
import trimesh

# Core settings
rootdir = 'path'
extension = ".zip"


for root, dirs, files in os.walk(rootdir):
    if not root.endswith(".zip"):
        for file in files:
            if file.endswith(".stl"):
                mesh = trimesh.load(file)

And I get the following error:

ValueError: File object passed as string that is not a file!

When I open the files one by one however, it works. What could be the reason ?


Solution

  • that's because file is the filename, not the full filepath

    Fix that by using os.path.join with the containing directory:

    mesh = trimesh.load(os.path.join(root,file))