comparison sat/tools/common/files_utils.py @ 3091:2e1c3d33099b

tools (common/files_utils): get_unique_name now uses and returns Path instances.
author Goffi <goffi@goffi.org>
date Fri, 20 Dec 2019 12:28:04 +0100
parents ab2696e34d29
children d6da17f6e4ce
comparison
equal deleted inserted replaced
3090:4f8bdf50593f 3091:2e1c3d33099b
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """tools to help manipulating files""" 20 """tools to help manipulating files"""
21 import os.path 21 from pathlib import Path
22 22
23 23
24 def get_unique_name(path): 24 def get_unique_name(path):
25 """generate a path with a name not conflicting with existing file 25 """Generate a path with a name not conflicting with existing file
26 26
27 @param path(unicode): path to the file to create 27 @param path(str, Path): path to the file to create
28 @return (unicode): unique path (can be the same as path if there is not conflict) 28 @return (Path): unique path (can be the same as path if there is no conflict)
29 """ 29 """
30 ori_path = path 30 ori_path = Path(path)
31 idx = 1 31 idx = 1
32 while os.path.exists(path): 32 while path.exists():
33 path = ori_path + "_" + str(idx) 33 path = ori_path.with_name(ori_path.name + f"_{idx}")
34 idx += 1 34 idx += 1
35 return path 35 return path