From 72e7c37ed24e9a8243974133c1263dba395a082e Mon Sep 17 00:00:00 2001 From: TEParsons Date: Wed, 16 Oct 2024 10:39:47 +0100 Subject: [PATCH 1/3] FF: Fix filename collision iterations --- psychopy/tools/fileerrortools.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/psychopy/tools/fileerrortools.py b/psychopy/tools/fileerrortools.py index 220aafda4a..36a70cf06e 100644 --- a/psychopy/tools/fileerrortools.py +++ b/psychopy/tools/fileerrortools.py @@ -38,10 +38,11 @@ def handleFileCollision(fileName, fileCollisionMethod): if extension: allowedExt = [extension] else: - allowedExt = [ - os.path.splitext(match)[1] for match in - glob.glob("%s*" % rootName) - ] + allowedExt = [] + for match in glob.glob("%s*" % rootName): + ext = os.path.splitext(match)[1] + if ext and ext not in allowedExt: + allowedExt.append(ext) # get extension (from options) with most files nFiles = 0 for ext in allowedExt: From 0b2af98a24180c6d1ea943ccad13496fdbc7d2f7 Mon Sep 17 00:00:00 2001 From: TEParsons Date: Wed, 16 Oct 2024 11:21:18 +0100 Subject: [PATCH 2/3] FF: Handle file collision by iteratively checking the name, rather than counting matches --- psychopy/tools/fileerrortools.py | 42 +++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/psychopy/tools/fileerrortools.py b/psychopy/tools/fileerrortools.py index 36a70cf06e..e9ce1785e5 100644 --- a/psychopy/tools/fileerrortools.py +++ b/psychopy/tools/fileerrortools.py @@ -9,6 +9,7 @@ """ import os import glob +from pathlib import Path from psychopy import logging @@ -32,28 +33,25 @@ def handleFileCollision(fileName, fileCollisionMethod): "fileCollisionMethod to overwrite.") raise IOError(msg % fileName) elif fileCollisionMethod == 'rename': - rootName, extension = os.path.splitext(fileName) - - # make extension iterable - if extension: - allowedExt = [extension] - else: - allowedExt = [] - for match in glob.glob("%s*" % rootName): - ext = os.path.splitext(match)[1] - if ext and ext not in allowedExt: - allowedExt.append(ext) - # get extension (from options) with most files - nFiles = 0 - for ext in allowedExt: - matchingFiles = glob.glob("%s*%s" % (rootName, ext)) - nFiles = max(nFiles, len(matchingFiles)) - - # Build the renamed string. - if not nFiles: - fileName = "%s%s" % (rootName, extension) - else: - fileName = "%s_%d%s" % (rootName, nFiles, extension) + # convert to a Path object + fileObj = Path(fileName) + # use a glob star if we don't have an ext + if not fileObj.suffix: + fileObj = fileObj.with_suffix(".*") + # get original file name + rootName = fileObj.stem + # get total number of sibling files to use as maximum for iteration + nSiblings = len(list(fileObj.parent.glob("*"))) + # iteratively add numbers to the end until filename isn't taken + i = 0 + while list(fileObj.parent.glob(fileObj.name)) and i < nSiblings: + i += 1 + fileObj = fileObj.with_stem(f"{rootName}_{i}") + # remove glob star from suffix if needed + if fileObj.suffix == ".*": + fileObj = fileObj.with_suffix("") + # convert back to a string + fileName = str(fileObj) # Check to make sure the new fileName hasn't been taken too. if os.path.exists(fileName): From 5001a36a87cbfef388b00308f9b28c82148d27d0 Mon Sep 17 00:00:00 2001 From: TEParsons Date: Wed, 16 Oct 2024 12:01:02 +0100 Subject: [PATCH 3/3] FF: pathlib with_... methods aren't implemented on Mac yet --- psychopy/tools/fileerrortools.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/psychopy/tools/fileerrortools.py b/psychopy/tools/fileerrortools.py index e9ce1785e5..17145d7e83 100644 --- a/psychopy/tools/fileerrortools.py +++ b/psychopy/tools/fileerrortools.py @@ -37,7 +37,7 @@ def handleFileCollision(fileName, fileCollisionMethod): fileObj = Path(fileName) # use a glob star if we don't have an ext if not fileObj.suffix: - fileObj = fileObj.with_suffix(".*") + fileObj = fileObj.parent / (fileObj.stem + ".*") # get original file name rootName = fileObj.stem # get total number of sibling files to use as maximum for iteration @@ -46,10 +46,10 @@ def handleFileCollision(fileName, fileCollisionMethod): i = 0 while list(fileObj.parent.glob(fileObj.name)) and i < nSiblings: i += 1 - fileObj = fileObj.with_stem(f"{rootName}_{i}") + fileObj = fileObj.parent / (f"{rootName}_{i}" + fileObj.suffix) # remove glob star from suffix if needed if fileObj.suffix == ".*": - fileObj = fileObj.with_suffix("") + fileObj = fileObj.parent / fileObj.stem # convert back to a string fileName = str(fileObj)