Forum: Poser Python Scripting


Subject: Replace a multiline XML string between two delimiters

HartyBart opened this issue on Sep 08, 2021 ยท 7 posts


bagginsbill posted Thu, 09 September 2021 at 1:37 PM

While this is correct, it can be a disaster if something goes wrong. If some other user is starting with this as a template and they change the code at line 18 to suit their purposes, it is easily possible that the program throws an exception (due to some temporary mistake on the user's part) which will result in the loss of all the file contents after the start tag.

It would be safer to open a DIFFERENT file for writing, write all the lines and close it, and ONLY THEN perform a delete and a couple file renames to safeguard that the original file BEFORE AND AFTER the replacement is retained, and that if a new content isn't produced properly, the original is not touched at all.

For similar reasons the f.close() should not be explicitly done by the program. Instead, perform the temp file production with a "with" statement. That will ensure that even if the program aborts, the file is properly closed. Otherwise, the Poser process will continue to have the file "open" and it may be difficult for the user to delete the file.


Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)