does anyone know how to delete a file from inside blender script? i tried to use os.remove, but it gives me an error:

TypeError: unlink: illegal type for path parameter

the path i put into the os.remove is the same path i use to load the file, where i save it into a variable. i tried changing the string variable to subtype = "FILE_PATH" but no effect. the loading goes fine, but if i input the same path to remove it doesn't work

is there any other way to do this?


1 Answer 1


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:

  1. some_file_path isn't set at that moment in time
  2. 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


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, ')')
            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

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()

def register():
    bpy.types.Scene.some_identifier = bpy.props.StringProperty()

def unregister():
    del bpy.types.Scene.some_identifier
  • $\begingroup$ Turns ou the os.remove() was correct it was just a dumb mistake of not defining the variable in the right place. Thanks for reminding me to check if the path is set. $\endgroup$
    – Joe
    Dec 8, 2015 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .