I have a script in Python (Windows 10) whose one of the functions is supposed to create a backup. Here’s the function:
def create_backups(self, file: str, counter: int = None) -> None:
counter = counter or 1
res = self.re_obj.match(file)
if res is None or len(res.groups()) == 0:
back_file = f"{file} ({counter}).{self.ext}"
else:
l_res = re.search("\w[\w\s]+?\.[a-zA-Z_]\w{2}", file)
file_name, file_ext = splitext(l_res.group())
back_file = f"{file_name}{file_ext} ({counter}).{self.ext}"
if exists(back_file) and counter < self.n_copies:
counter += 1
self.create_backups(back_file, counter)
with open(file, "rb") as f_src, open(back_file, "wb") as f_dst:
shutil.copy2(f_src, f_dst)
if self.verbose:
print(f"Copied {file} to '{back_file}'")
It’s supposed to make several copies (parametrised). As you can see it invokes itself in case the back_file
already exists. The pattern is following:
test.txt
test.txt (1).sav
test.txt (2).sav
...
test.txt (n).sav
with n
depending on the parameter n_copies
.
If I convert it to a exe
file, it still works in PowerShell
. However, if I try to run it from another program, it raises PermissionError
exception on the second copy test.txt (1).sav
. For instance, I use FreeCommander XE and I am trying to run the script as follows:
The parameters are following:
-f
file name and FreeCommander subsitutes%ActivSel%
with the selection-n
no. of copies-e
extension, e.g.sav
-b
backup method
If I run an equivalent parametrised script from PowerShell
:
backup.exe -f "test.txt" -n 14 -e "sav"
everything works. It is in the FreeCommander where I’ve got PermissionError
exactly at the line of
shutil.copy2(f_src, f_dst)
I also tried to run FreeCommander as administrator with no difference.
I am not sure how to sort this out. Any ideas?