Python 自动给重名文件名添加序号

检查给定的文件路径是否已经重名,并自动添加序号,递增数字。 Code on Github Gist def auto_rename_exist_filename(file_path): """ For example: a.txt, a(1).txt, a(2).txt Source: https://gist.github.com/nodewee/9ae9b0b461b4bcaf30bc2b84ca8c4743.js """ if not os.path.exists(file_path): return file_path dir_path = os.path.dirname(file_path) src_filename = os.path.basename(file_path) [file_title, ext_name] = os.path.splitext(src_filename) pattern = r"\((\d+?)\)$" r = re.search(pattern, file_title) if r: serial =