1

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 ?

Cenk_Mitir
  • 113
  • 11

2 Answers2

2

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))
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

This is not a direct answer to your question. However, you might be interested in noting that there is now a less complicated paradigm for this situation. It involves using the pathlib module.

I don't use trimesh. I will process pdf documents instead.

First, you can identify all of the pdf files in a directory and its subdirectories recursively with just a single line.

>>> from pathlib import Path
>>> for item in path.glob('**/*.pdf'):
...     item
... 
WindowsPath('C:/Quantarctica2/Quantarctica-Get_Started.pdf')
WindowsPath('C:/Quantarctica2/Quantarctica2_GetStarted.pdf')
WindowsPath('C:/Quantarctica2/Basemap/Terrain/BEDMAP2/tc-7-375-2013.pdf')    WindowsPath('C:/Quantarctica2/Scientific/Glaciology/ALBMAP/1st_ReadMe_ALBMAP_LeBrocq_2010_EarthSystSciData.pdf')
WindowsPath('C:/Quantarctica2/Scientific/Glaciology/ASAID/Bindschadler2011TC_GroundingLines.pdf')

WindowsPath('C:/Quantarctica2/Software/CIA_WorldFactbook_Antarctica.pdf')
WindowsPath('C:/Quantarctica2/Software/CIA_WorldFactbook_SouthernOcean.pdf')
WindowsPath('C:/Quantarctica2/Software/QGIS-2.2-UserGuide-en.pdf')

You will have noticed that (a) the complete paths are made available, and (b) the paths are available within object instances. Fortunately, it's easy to recover the full paths using str.

>>> import fitz
>>> for item in path.glob('**/*.pdf'):
...     doc = fitz.Document(str(item))
... 

This line shows that the final pdf document has been loaded as a fitz document, ready for subsequent processing.

>>> doc
fitz.Document('C:\Quantarctica2\Software\QGIS-2.2-UserGuide-en.pdf')
Bill Bell
  • 21,021
  • 5
  • 43
  • 58