关于最近做农行的苦力活后写的代码

import os
import shutil


def move_and_rename(src, dest_dir):
    """移动文件到目标目录,如果存在同名文件则自动重命名"""
    filename = os.path.basename(src)
    base, ext = os.path.splitext(filename)
    counter = 1
    new_filename = filename
    dest_path = os.path.join(dest_dir, new_filename)

    # 检查目标文件是否存在,如果存在则添加后缀
    while os.path.exists(dest_path):
        new_filename = f"{base}_{counter}{ext}"
        dest_path = os.path.join(dest_dir, new_filename)
        counter += 1
    shutil.move(src, dest_path)


def process_directory(root_dir):
    for dir_name in os.listdir(root_dir):
        dir_path = os.path.join(root_dir, dir_name)
        if not os.path.isdir(dir_path):
            continue  # 跳过非目录项

        # 处理“备案”文件夹
        beian_path = os.path.join(dir_path, '备案')
        if os.path.exists(beian_path):
            for filename in os.listdir(beian_path):
                src = os.path.join(beian_path, filename)
                if os.path.isfile(src):
                    move_and_rename(src, dir_path)
            # 删除“备案”文件夹
            shutil.rmtree(beian_path)

        # 处理“备案2”文件夹
        beian2_path = os.path.join(dir_path, '备案2')
        if os.path.exists(beian2_path):
            for subfolder_name in os.listdir(beian2_path):
                subfolder_path = os.path.join(beian2_path, subfolder_name)
                if os.path.isdir(subfolder_path):
                    for filename in os.listdir(subfolder_path):
                        src = os.path.join(subfolder_path, filename)
                        if os.path.isfile(src):
                            move_and_rename(src, dir_path)
                    # 删除子文件夹
                    shutil.rmtree(subfolder_path)
            # 删除“备案2”文件夹
            shutil.rmtree(beian2_path)


if __name__ == '__main__':
    root_dir = '农行'  # 请替换为实际路径
    process_directory(root_dir)

其中大概得结构基本上是这样的:

要求处理之后变成这样:

 代码解释

 代码用ai写的,不得不承认,ai还是越来越好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值