I know, it is a stupid question, but is it possible that you kill the level before you save it?

I mean, do you unload the level? If yes, then he can not save the level.
There must be any kind of error in the script. Otherwise it makes no sense.

Have you checked if the savegame is in the folder? With and without levels-option.

Which OS do you use? And in which folder do you write? I mean, is it in the program folder? or anywhere else? Because of rights.

What kind of level has the game?
You can try to make a simple level with wed and start it instead of your level and try to save it with your script. I think it will work, because I don't see a problem with the lines you have posted.