That's right os.remove() is the way to go, on Unix (Linux and OSX too) and Windows. See this answer on stackoverflow
That error likely occurs because you are calling os.remove(some_file_path)
where:
some_file_path
isn't set at that moment in time
- or the filepath can't be resolved by os.remove().
Try printing the value of some_file_path
just ahead of calling os.remove()
, most likely you'll either get an empty string or some modified path.
See this working example.
- This will add a panel to properties called
Hello World Panel
,
- it has a file selector and stores the filepath as a scene variable called
some_identifier
.
- The button 'Remove File' removes the file identified by scene property
code:
bl_info = {
"name": "FILL ME IN",
"author": "",
"version": (0, 1),
"blender": (2, 7, 6),
"category": "3D View"
}
import os
import bpy
from bpy_extras.io_utils import ExportHelper
class WMFileRemover(bpy.types.Operator):
bl_idname = "wm.remove_file"
bl_label = "Remove File"
def execute(self, context):
fp = context.scene.some_identifier
if fp:
print('os.remove(', fp, ')')
os.remove(fp)
else:
self.report({'ERROR'}, 'filepath is an empty string')
return {'FINISHED'}
class WMFileSelector(bpy.types.Operator, ExportHelper):
bl_idname = "something.identifier_selector"
bl_label = "some folder"
filename_ext = ""
def execute(self, context):
fdir = self.properties.filepath
context.scene.some_identifier = fdir
return{'FINISHED'}
class WMFilePanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
def draw(self, context):
layout = self.layout
scn = context.scene
# ui
col = layout.column()
row = col.row(align=True)
row.prop(scn, 'some_identifier', text='directory:')
row.operator("something.identifier_selector", icon="FILE_FOLDER", text="")
col = layout.column()
col.label(scn.some_identifier)
col.operator("wm.remove_file")
def register():
bpy.types.Scene.some_identifier = bpy.props.StringProperty()
bpy.utils.register_module(__name__)
def unregister():
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.some_identifier