import os
def safe_path(path):
name, ext = os.path.splitext(path)
while True:
current_name = name + ext
if not os.path.exists(current_name):
return current_name
name += '_'
if __name__ == '__main__':
open('/tmp/1.txt', 'a').close()
open('/tmp/1_.txt', 'a').close()
assert safe_path('/tmp/1.txt') == '/tmp/1__.txt'