os.system(<batchfile>) empties copied file
-
Sorry for the new trouble: My Python script includes a line os.system(‘cmd /c “MyCopy.bat”’). This batch-file works flawlessly on OS-level (Win10pro), but after running the script within np++, a batch-file-copied file gets 0 bytes at its source-location, while the target is non-empty and fine.
Thanks again!
-
@Peter-Greistorfer said in os.system(<batchfile>) empties copied file:
os.system(‘cmd /c “MyCopy.bat”’)
You are talking about the PythonScript plugin, right?
Because otherwise your question is off-topic and not right for this forum.I suppose you’d have to tell us more about the contents of
MyCopy.bat
, but I’m hesitant to request that because it may drive us into off-topic-land.Also, I’m going to guess that you are doing things in the batch file that you really should do via Python. Batch files are fine when you have no need for anything else, but one presumes you are using PythonScript for a reason here… Trying to have them coexist as two parts of one solution…well, it can work, but it can be awkward and fragile.
-
Yes, the plugin, of course.
The batch file copies the file (which then gets emptied) to a bak-directory, where it is renamed to generate different bak/save-states yielding something like MyFile_date_# (where # is a counter). Some lines, yes, but nothing special in terms of DOS.
-
Yea… It’s hard to say.
I certainly have never seen any behavior like that, but I can’t say I’ve ever gotten really deep into a solution architected like that.
I guess you’ll just have to experiment and poke and prod around with it (if you’re so inclined) in order to see what you can determine, if anything. -
Well, you have given us nothing to go on as to how the PythonScript was written, or how the batch file was implemented. And the only Python command you gave us was a generic Python command, which is not specific to or dependent on the PythonScript plugin itself.
So, we have no useful information to help base our reply on, and no reason to believe it’s a problem with the Notepad++-specific requirements of the PythonScript plugin, rather than just a generic Python language problem that is agnostic of the editor or the plugin, and/or the Python interface with the cmd shell and batch file (again, having nothing to do with Notepad++ or its plugin).
As @Alan-Kilborn said, I am not sure why you would even need to shell out to a batch file to make a copy of a file: the Python language can handle that without difficulty. Even more, the PythonScript plugin provides the
editor.saveAs(filename)
command, which allows saving the active document to another name and making the copy open in Notepad++ refer to the new name instead of the old; and provides theeditor.saveAsCopy(filename)
command, which saves the active document to another name _without changing which file the editor currently has loaded.Example: if you had “local.txt” open in the editor,
editor.saveAs("remote.txt")
would end up with a copy at “remote.txt”, and the editor would be editing the “remote.txt”; if you instead usededitor.saveAsCopy("remotecopy.txt")
, then there would be a new copy of the file, but the editor would still have “local.txt” open. I believe thesaveAsCopy()
sounds like your backup scheme.(Actually, I’m not even sure that your backup scheme sounds like it improves on Settings > Breferences > Backup > Backup on save with the Verbose backup enabled and Custom Backup Directory set. But your backup needs are better known to you than to us or the developers, and you may have omitted enough details that I couldn’t tell the difference.)
So, to sum up: 1) You haven’t given us enough to go on to be able to meaningfully answer the specifics of your question as to why you are getting a 0-byte file. 2) Python and/or Python Script should be able to eliminate the need to shell out to a bat file. 2b) For making backup files, the
editor.saveAsCopy()
will likely provide all the functionality you want, without having to shell out to a batch file. 3) Notepad++'s built-in backup system might eliminate the need for the script at all. -
Whoops.
Anywhere I wrote
editor.saveAs
oreditor.saveAsCopy
, I meantnotepad.saveAs
andnotepad.saveAsCopy
. Sorry. -
@PeterJones said in os.system(<batchfile>) empties copied file:
or how the batch file was implemented
As I said, I was torn on venturing too far down that perhaps off-topic road…
I am not sure why you would even need to shell out to a batch file to make a copy of a file: the Python language can handle that without difficulty.
I can maybe see this if someone has an existing process (probably in place for years) and they’re just trying to bring that existing functionality into some newer Notepad++ “wrapper”…maybe.
So, to sum up
But yes, in general I agree with Peter’s points.
-
@Alan & Peter: I see and understand your view. My low-information policy was based on the wish not to stress you all that much. So well, I’ll poke around and if I find a solution, of course, I’ll post it here.
Many thanks for your incentives and - not to forget: Happy Easter!