# Quick way to get current opened filename in a script?

I'm looking for an efficient way to get the current .blend file name, in a python script.

I know how to get its path, with bpy.path.abspath('//'), but it returns only the current path, without the file name. I'm sure it's possible, but I can't find out how.

It's in bpy.data.filepath.

This is the absolute path to the currently open blend file, see is_saved, and is_dirty too.

You can get the filename with bpy.path.basename(bpy.context.blend_data.filepath). This will return it as a string without the path attached. Use for Windows compatibility and some more info here.

If you run Python script directly from a Unix Shell with a Blender as interpreter you may use one of below:

#!blender -P
import os,sys

print("OS.PATH.ABSPATH(__FILE__): "+ os.path.abspath(__file__))
print("SYS.ARGV[-1]: " + sys.argv[-1])


os.path.abspath(__file__) will give you an absolute path (relpath() available as well).

sys.argv[-1] will give you a relative path.