Is it possible to set bpy.data.filepath without saving the file? I have some code which requires opening and closing blend files. The code has to save a temporary copy of the open file before opening other files and doing stuff to them. After the code is finished it reloads the temporary save of the original file.

The problem is, if the user goes to save their file (after the code has been carried out) it will save to the temporary location, of course they could change the save location manually but that would insert an unnecessary amount of entropy into this process.

I could also simply save the open file before the code was run, but I would like to avoid forcing that on the user. All that's holding me back is not being able to change the file's internal save location after all is finished.

If I could write to bpy.data.filepath (rather than only read it) problem would be solved. Anyone know of a way to set this value without actually saving the file with: bpy.ops.wm.save_as_mainfile() ?

As it stands I've developed a complicated method of working around this, but involves renaming and copying files. If I have to use it I will but hoping someone might know a slimmer solution here.

Also I'm aware there is a similar thread "Clear saved filepath" but I was hoping to explore this further. I just want to know is there something I'm missing with setting the blend file's save path.

EXPLANATION OF HOW IT'S DIFFERENT FROM "CLEAR SAVED FILEPATH":
Let's see, that thread doesn't solve the problem. The end.
The bpy.data.filepath property reflects the current blendfile's location, which is why it is read only.

Not sure I understand your problem fully, but here's a suggestion:
You can save the file implicitly (without asking the user to provide a save path), and decide yourself on an arbitrary temporary path for your save. By providing the bpy.ops.wm.save_as_mainfile() operator a filepath parameter, you can save directly to any path you desire.

bpy.ops.wm.save_as_mainfile( filepath = MYPATH )


You can later load that file implicitly again, with the opposite operator:

bpy.ops.wm.open_mainfile( filepath = MYPATH )


This way, you don't need to care what the value of bpy.data.filepath is.