saveAs and saveAsCopy do not save in original file path
I observe a weird issue I do not know how to debug:
I run for years Notepad++ as 32bit version in portable mode. Despite having a 64 bit machine I did not switch because of some plugins I used. But recently I took again a look and decided I could now switch. So, I installed the 64bit version, luckily with a backup of my original setup.
So, now for PythonScript: I use it in the moment for this script
import re
# several editor.rereplace commands
notepad.saveAsCopy("backup.txt")
notepad.menuCommand(42059) # sort content alphabetically
notepad.saveAs("newname.txt")
In my original setup this saves the files as expected in the same path like the opened file.
But with the 64bit version the copies will be saved in path of notepad++.exe! Apparently, the active path is lost.
Why I do not know how to debug:
- I have copied the settings and intentionally changed the directory names (64bit version in path of original setup), no change.
- For testing purposes I had changed to open more than one instance, but reset to default, i.e. only one instance, does not work.
- I wanted to test, whether it is an issue of 32 vs. 64 bit, and disabled all plugins in 32bit without the default ones and PythonScript, of course. But now I observe the wrong behaviour in the 32 bit version, too! This is reproducible, I just have to switch the plugin directories (backup and test).
Edit: Oh, I forgot important information. Both Notepad++ and Pythonscript are in the most recent stable versions, i.e. Npp 4.9.5 and Pythonscript 1.5.4. OS is Windows 7.
As the programmer, I'd say the burden on you to make sure your files get saved to the desired place.
Meaning that you should provide the full path when using notepad.saveAs() or notepad.saveAsCopy().
The fact that maybe some time in the past this worked without specifying the full path probably qualifies it for the disclaimer of "undefined behavior" that you should not have relied on.
In my original setup this saves the files as expected in the same path like the opened file.
It isn't that hard to do this correctly:
from Npp import notepad
import os
d = notepad.getCurrentFilename().rsplit(os.sep, 1)[0]
notepad.saveAsCopy(d + os.sep + 'backup.txt')
notepad.saveAs(d + os.sep + 'newname.txt')
@Speravir It would be helpful to have some version information on windows version, N++, python plugin and the plugin list working vs. not working.
@sasumner:
make sure your files get saved to the desired place. Meaning that you should provide the full path when using
notepad.saveAs()ornotepad.saveAsCopy(). […] It isn't that hard to do this correctly:
Thanks for the code. It is easy for a coder like you, but not for someone like me who is heavily dependent on good documentation! I was just happy when I got this simple script working and it was much beyond my abilities when it stopped working with the different N++.
This said add a note regarding the path dependency and your code snippet into the docs for PythonScript and then point to this from all relevant places in the docs. I didn’t even know about a module os, but also would not have known how to split between directory path and file.
from Npp import notepad
This is just for safety, right? Because somewhere in the docs you write that the module notepad is imported within the startup script. At least the code works also without this.
So, the issue was more a PEBKAC and doc issue, and from my side the ticket is solved. Feel free to close it yourself or let it open as a reminder for updating the documentation.
@chcg:
It would be helpful to have some version information on windows version, N++, python plugin
Oh (blushing), right, I forgot this. I added it in the first posting, but as said from my side the ticket is solved.
and the plugin list working vs. not working.
Still interested? I could try to find out whether another plugin helps for this weird thing that it worked in first place without any path addition.
A note regarding my OS and Python: I am unfortunately stuck with it. I noticed in commit notes that you seemingly want to use Python 3.9.5 in the future. Alas, 3.9x versions of Python do not work in Windows 7 anymore (3.8.x do). I think, though, that as long Notepad++ itself runs in Windows Vista and Win7 also the plugins should run in these OS versions. (Edit:) See also #203 for a consequence.