# Writing to file script failed

I keep getting an error from this scrip and am unsure where I am making a mistake.

import bpy
import os

for ob in bpy.data.objects:
file_object = open('blender_coordinate.txt', 'w') as f:
f.write(ob.name)
f.write(', ')
f.write(ob.location)
f.write(', ')
f.write(ob.rotation)
f.write('\n')
file_object = close()
return {'Finished'}


Here is the error i am recieving

  File "Text.py", line 5
file_object = open('blender_coordinate.txt', 'w') as f:
^
SyntaxError: invalid syntax

location: <unknown location>:-1

Python script fail, look in the console for now...


I am still new to scripting and appreciate any help.

It seems that you mixed two methods. You can either use :

file_object = open('myfilename','w')
file_object.write(ob.name)
file_object.close()


or

with open('myfilename','w') as file_object:
file_object.write(ob.name)
#no need to close


The second option is better (see https://www.pythonforbeginners.com/files/with-statement-in-python).

Both options only require you to import bpy

• Consider accepting the answer @Nburgess – brockmann Mar 17 at 18:57
• I dont know how to do that. – Nburgess Mar 18 at 13:26