批量重名命某个目录下文件名


import os
# 递归获取文件

def rename_file(path,new_file):
    for oldname in new_file:
        oldname = str(oldname)
        if '【程序员教程吧 论 坛 www.cxyjc8.com】' in oldname:
            newname = oldname.replace('【程序员教程吧 论 坛 www.cxyjc8.com】', '')
            oldname = os.path.join(path, oldname)
            newname = os.path.join(path, newname)
            try:
                os.rename(oldname, newname)
            except Exception as e:
                print(e)
                print('rename file fail\r\n')

def recursion_get_file(paths):
    path = paths + "/"
    first_dir_list = os.listdir(path)
    print(first_dir_list)
    new_file = [file for file in first_dir_list if os.path.isfile(path + file)]

    if new_file:
        # 重命名
        rename_file(path, new_file)
    dirs = [str(path + file) for file in first_dir_list if os.path.isdir(path + file)]
    if dirs:
        for dir in dirs:
            recursion_get_file(dir)


if __name__=="__main__":
    # 需要重命名文件根目录
    root_paths=r'E:\07_2020黑马java会员版2.1完整版\12-就业课(2.1)-消息中间件-RabbitMQ - 副本'
    recursion_get_file(paths=root_paths)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值