"""Implements the function providing names for the output .py.:author: Michel Bierlaire:date: Tue Mar 26 16:48:40 2019"""importloggingfrompathlibimportPathlogger=logging.getLogger(__name__)
[docs]defget_new_file_name(name:str,ext:str)->str:""" Generate a file name that does not exist. :param name: name of the file. :param ext: file extension. :return: name.ext if the file does not exist. If it does, returns name~xx.ext, where xx is the smallest integer such that the corresponding file does not exist. It is designed to avoid erasing output .py inadvertently. """file_name=name+'.'+extthe_file=Path(file_name)number=int(0)whilethe_file.is_file():file_name=f'{name}~{number:02d}.{ext}'the_file=Path(file_name)number+=1returnfile_name