# How to set render filepath using blend filename in python

Let say I have blend file called test01.blend, then I want to automatically assign render filepath via python to /media/data/render/[current blend filename]/[current blend filename]_.

So, it should look like this /media/data/render/test01/test01_

I'm trying to access it via bpy.data.filepath, but I got only full file path for current blend file not blend file name.

Thanks for your help

• See this post – gandalf3 Nov 7 '13 at 9:58

## 1 Answer

From iKlsR's answer:

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.

You can then assign the render output path with it:

import bpy

filename = bpy.path.basename(bpy.data.filepath)
if filename:
bpy.context.scene.render.filepath = filename


Note that basename will include the file extension (.blend). You can strip it using os.path.splittext():

import os
import bpy

filename = bpy.path.basename(bpy.data.filepath)
filename = os.path.splitext(filename)[0]

if filename:
bpy.context.scene.render.filepath = os.path.join("/media/data/render", filename, filename + "_")

• is there any difference between bpy.context.blend_data.filepath and bpy.data.filepath? – CodeManX Nov 7 '13 at 10:08
• it'll print .blend also in render filepath, any workaround to solve this? – aditia Nov 7 '13 at 10:08
• @aditia Added a workaround – gandalf3 Nov 7 '13 at 10:10
• @CoDEmanX Good question. I don't know, but I don't see any.. (perhaps you should ask it? ;) ) – gandalf3 Nov 7 '13 at 10:11
• maybe make it more generic and use os.path.splitext[0] instead of replace('.blend', '')? – CodeManX Nov 7 '13 at 10:47