# How to create a .blend in specific directory and write objects on it?

For my addon, I want to pick the selected armature and save in an external file, so I wrote the following operator for doing it:

class SaveArmatures(bpy.types.Operator):
bl_idname = "armature_snippets.save_armatures"
bl_label = "Save Armatures"
bl_description = ""
bl_options = {"REGISTER", "UNDO"}

@classmethod
def poll(cls, context):
if context.active_object:
return context.active_object.type == "ARMATURE"

def execute(self, context):
ob = {context.active_object}

# Here the split method is just to get the first term of the string
#because it is a multifile addon

bpy.data.libraries.write(os.path.join(path, "Snippets.blend"), ob, fake_user = True)
return {"FINISHED"}


But when I run it raises this error. Obviously I need to create the file first but how to do it?

Traceback (most recent call last):
bpy.data.libraries.write(os.path.join(path, "Snippets.blend"), ob, fake_user = True)
OSError: Error: Cannot open file C:\Users\Jea\Desktop\snippets\armature_snippets\Snippets.blend@ for writing: No such file or directory

location: <unknown location>:-1

• – Jeacom Sep 5 '17 at 14:26
• yeah, and need to creafe the file from python. Thats the problem. – Jeacom Sep 5 '17 at 14:43
• Take a look at stackoverflow.com/q/23326310/6590534 – Dimali Sep 5 '17 at 14:52
• Doh, should have tested this first, rather than assume you had an existing path. Can confirm that library.write(..) creates a new file, as long as you have a valid path to the folder, as outlined by @aliasguru – batFINGER Sep 5 '17 at 18:36
• Wieeird, why not create the file in one go since it creates the directory. – Jeacom Sep 5 '17 at 22:54

So in your given example, check if the folder C:\Users\Jea\Desktop\snippets\armature_snippets\ exists using the os.path module, and if not, create it:
if not os.path.exists(directory):