bpy.context.space_data.text throwing error from command line
In the text editor window the [filepath] refers to file location where you save the script.
In the console window there is no such structure.

import bpy
import datetime

print(datetime.datetime.today())
print(bpy.context.space_data.text.filepath)
print(bpy.data.filepath)


The above was executed in the Script window.  You can compare the results.