# Error importing several meshes from single dir

I have been trying to figure out an error message I get with attempting to script blender to import all stl's from a dir, edit them, then export them back to the same dir with the filename appended. I have had luck doing this with a single specified stl, but cannot get this to work so far with multiple files. Here is the code I am using to try this with multiple files (it is very hamfisted, this is my first experience with python or scripting):

import os
import bpy
import sys

file_path = ("C:\stlfiles")
dir = os.listdir("C:\stlfiles")

for file_path in dir:
if file_path.lower().endswith('.stl'):
try:
bpy.ops.object.delete()
bpy.ops.import_mesh.stl(filepath=file_path, filter_glob="*.stl")

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.tris_convert_to_quads()
bpy.ops.object.editmode_toggle()

bpy.ops.object.modifier_add(type='BEVEL')
bpy.context.object.modifiers["Bevel"].width = 0.056
bpy.context.object.modifiers["Bevel"].segments = 2
bpy.context.object.modifiers["Bevel"].profile = 1
bpy.context.object.modifiers["Bevel"].limit_method = 'ANGLE'
bpy.context.object.modifiers["Bevel"].angle_limit = 0.698132
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.context.object.modifiers["Subsurf"].levels = 2
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Bevel")
bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Subsurf")

bpy.context.object.scale[0] = 10
bpy.context.object.scale[1] = 10
bpy.context.object.scale[2] = 10

bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_all(action='INVERT')

bpy.ops.export_mesh.stl(filepath=file_path + '_exported.stl', check_existing=True,
filter_glob="*.stl")
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_all(action='INVERT')
except:

print ("Couldn't open file")


Here is the error I am getting (test01.stl, and test02.stl are in the stsfiles dir):

{'CANCELLED'}
Error: Traceback (most recent call last):
File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\addons\io_mesh_stl\__init__.py", line 151, in execute
tris, tri_nors, pts = stl_utils.read_stl(path)
File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\addons\io_mesh_stl\stl_utils.py", line 261, in read_stl
with open(filepath, 'rb') as data:
FileNotFoundError: [Errno 2] No such file or directory: 'test01.stl'

location: C:\Program Files\Blender Foundation\Blender\2.79\scripts\modules\bpy\ops.py:189

Couldn't open file
{'CANCELLED'}
Error: Traceback (most recent call last):
File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\addons\io_mesh_stl\__init__.py", line 151, in execute
tris, tri_nors, pts = stl_utils.read_stl(path)
File "C:\Program Files\Blender Foundation\Blender\2.79\scripts\addons\io_mesh_stl\stl_utils.py", line 261, in read_stl
with open(filepath, 'rb') as data:
FileNotFoundError: [Errno 2] No such file or directory: 'test02.stl'


Can you point me in the right direction to resolve this please? Thanks in advance.

## 1 Answer

os.listdir only return name and extension and subdir for a Dir, you didn't indicate the relative directory path or absolute path for filepath to search the file by name.

You could use os.path.join() to join your path:

dir_path = ("C:\stlfiles")
files = os.listdir(dir_path) # if you just create a reference to dir, why not use it directly?

for file_name in files:
if file_name.lower().endswith('.stl'):
full_file_path = os.path.join(dir_path,file_name)
pass #do something to full_file_path