python实现下载文件路径自动添加(1)的递增路径
一、代码
实现下载路径如果已存在就自动添加(1),如果(1)也存在就变成(2),依次递增
def is_file(file_name): if not os.path.exists(file_name): return file_name num = 1 if re.findall("\((\d)\)", file_name): num = re.findall("\((\d)\)", file_name) new_num = int(num[0]) + 1 file_name=file_name.replace(num[0],str(new_num)) return is_file(file_name) path_list = file_name.split(".") file_name = path_list[0] + "({}).".format(num) + path_list[1] return is_file(file_name) save_path=r"C:\temp\mail.eml" res=is_file(save_path) print(res)