handle_filename_collision(filename,
filenames)
| source code
|
Avoid filename collision, add a sequence number to the name when
required. 'file.txt' will be renamed into 'file-01.txt' then
'file-02.txt' ... until their is no more collision. The file is not added
to the list.
Windows don't make the difference between lower and upper case. To
avoid "case" collision, the function compare
filename.lower() to the list. If you provide a list in lower
case only, then any collisions will be avoided.
- Parameters:
filename (str) - the filename
filenames (list or set) - a list of filenames.
- Returns: str
- the filename or the appropriately indexed
filename
>>> handle_filename_collision('file.txt', [ ])
'file.txt'
>>> handle_filename_collision('file.txt', [ 'file.txt' ])
'file-01.txt'
>>> handle_filename_collision('file.txt', [ 'file.txt', 'file-01.txt',])
'file-02.txt'
>>> handle_filename_collision('foo', [ 'foo',])
'foo-01'
>>> handle_filename_collision('foo', [ 'foo', 'foo-01',])
'foo-02'
>>> handle_filename_collision('FOO', [ 'foo', 'foo-01',])
'FOO-02'
|