How to get the directory of open blend file? (from Python) What's the best way to get the full directory of the current Blender file in Python?

It should be cross-platform.
Path Utilities module provides bpy.path.abspath("//") where the "//" prefix is a Blender specific identifier for the current blend file.

You can also do...


bpy.path.abspath("//my/file.txt")
bpy.path.abspath("//my\\file.txt") on Windows, with backslash escaped
bpy.path.abspath(r"//my\file.txt") on Windows, using python raw string
bpy.path.abspath("//../file.txt")  to go back a directory


This is used by all internal paths in blender, image, video, render, pointcache etc - paths. so blend files can reference paths relative to each file.

Worth noting that the path may be an empty string, so you may want to check bpy.data.is_saved first.