# Why does the File Browser's 'filepath' property include the blend file name once the file has been saved?

I am puzzled by a weird behaviour of the Blender File Browser (v2.81) that I use to select the path to a folder containing the data my script processes:

• when it is invoked from a Blender project that was never saved, self.filepath returns the path selected e.g. my/selected/path/. This is the behaviour I am expecting.

• when it is invoked once the Blender project file as been saved (foo.blend), then self.filepath returns the path selected and appends the name of the project to it my/selected/path/foo.blend

I am getting around this by stripping foo.blend but I am puzzled by this.

import bpy
import os
from bpy_extras.io_utils import ImportHelper
from bpy.types import Operator

class OT_TestOpenFilebrowser(Operator, ImportHelper):
bl_idname = "test.open_filebrowser"
bl_label = "Select"
def execute(self, context):
filename, extension = os.path.splitext(self.filepath)
print('Selected:', self.filepath)
print('File name:', filename)
print('File extension:', extension)

return {'FINISHED'}

def register():
bpy.utils.register_class(OT_TestOpenFilebrowser)

def unregister():
bpy.utils.unregister_class(OT_TestOpenFilebrowser)

if __name__ == "__main__":
register()
bpy.ops.test.open_filebrowser('INVOKE_DEFAULT')


Run from a never saved Blender project:

Run again after saving Blender project:

Why is that? Am I missing a setting or parameter?

• self.filepath contains the concatenation of the current path and the filename. There is no check whether or not that file actually exist, how your script is currently implemented. You can enter any string you want in the filename field or set it to an empty string. Perhaps it's easier for us to help if we know what you're trying to do. Dec 1 '19 at 17:38
• IMO the filebrower is a tricky beast. What you are seeing is context.space_data.params.filename which you typed in when you saved the file. Other undocoed stuff like multi select files ie can define a files collection property of OperatorFileListElement see blender.stackexchange.com/a/39855/15543 (may be somewhat outdated) Suggest too checking out the code of bpy_extras.io_utils.py Dec 1 '19 at 18:39

Thanks to all for the input. I am answering my own question.

I wanted to have in my AddOn a way to choose a folder containing some input files. It turns out that it was very easy using bpy.props StringProperty as panel property:

import_path: StringProperty(
name = "Path",
description = "Path to the folder containing the files to import",
default = "",
subtype = 'DIR_PATH'
)


The DIR_PATH (or FILE_PATH, if expecting files) subtype is presenting a file system browser and retrieve the relative path, one just have to convert it to a absolute path in the operator:

bpy.path.abspath(self.import_path)


see here