python 新生成一个文件,并检测是否存在同名文件,存在则重命名

# 递归检测文件文件是否存在,存在则重命名文件
def check_file_exists(check_file, original_file=None, num=1):
    if os.path.exists(check_file):
        original_file = original_file if original_file else check_file
        _splitext = os.path.splitext(original_file)
        new_file_name = _splitext[0] + str(num) + _splitext[1]
        new_file_name = check_file_exists(new_file_name, original_file, num + 1)
        return new_file_name
    return check_file


检测目录

path

执行

python

输出

python

posted @   二月雪  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
点击右上角即可分享
微信分享提示