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.
Test code (courtesy of this page):
import bpy
import os
from bpy_extras.io_utils import ImportHelper 
from bpy.types import Operator

class OT_TestOpenFilebrowser(Operator, ImportHelper):
    bl_idname = &quot;test.open_filebrowser&quot; 
    bl_label = &quot;Select&quot; 
    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__ == &quot;__main__&quot;: 
    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?
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