comparison sat/tools/common/files_utils.py @ 2593:d78eff6b4487

tools (common): new files_utils module: get_unique_name find a name not conflicting with an existing file
author Goffi <goffi@goffi.org>
date Fri, 25 May 2018 10:52:01 +0200
parents
children 56f94936df1e
comparison
equal deleted inserted replaced
2592:c0401a72cbb4 2593:d78eff6b4487
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 """tools to help manipulating files"""
21 import os.path
22
23
24 def get_unique_name(path):
25 """generate a path with a name not conflicting with existing file
26
27 @param path(unicode): path to the file to create
28 @return (unicode): unique path (can be the same as path if there is not conflict)
29 """
30 ori_path = path
31 idx = 1
32 while os.path.exists(path):
33 path = ori_path + u'_' + unicode(idx)
34 idx += 1
35 return path