- try:
- os.link(old_path, new_path)
- except OSError:
- # destination file exists, don't overwrite it
- # TODO: this should probably be more informative
- return redirect(file_new, filename)
- else:
- os.unlink(old_path)
- audiobook.delete()
+ success = False
+ try_new_path = new_path
+ try_number = 0
+ while not success:
+ try:
+ os.link(old_path, try_new_path)
+ except OSError:
+ # destination file exists, don't overwrite it
+ try_number += 1
+ parts = new_path.rsplit('.', 1)
+ parts[0] += '_%d' % try_number
+ try_new_path = ".".join(parts)
+ else:
+ os.unlink(old_path)
+ audiobook.delete()
+ success = True