提取文件夹中所有文件,通过python实现

首先安装必要的库,在终端中通过pip install 安装

import os
import shutil
# 导入必要的库

找到你的文件夹路径复制进去

目标文件夹会在之后进行判断,如果不存在路径可以直接创建,如果目标文件夹中已存在同名文件,可以选择重命名或跳过

# 源文件夹路径列表
source_folders = ['F:/4.procedure/bearing fault diagnosis/database/CRWU']

# 目标文件夹路径
destination_folder = 'F:/4.procedure/bearing fault diagnosis/database/CRWU/1'

 mian函数如下所示:

if not os.path.exists(destination_folder):
    os.makedirs(destination_folder)

for folder in source_folders:
    for root, dirs, files in os.walk(folder):
        for file in files:
            source_file = os.path.join(root, file)
            destination_file = os.path.join(destination_folder, file)
            
            # 如果目标文件夹中已存在同名文件,可以选择重命名或跳过
            if os.path.exists(destination_file):
                base, extension = os.path.splitext(file)
                counter = 1
                new_destination_file = os.path.join(destination_folder, f"{base}_{counter}{extension}")
                while os.path.exists(new_destination_file):
                    counter += 1
                    new_destination_file = os.path.join(destination_folder, f"{base}_{counter}{extension}")
                destination_file = new_destination_file
            
            shutil.copy2(source_file, destination_file)

print("All files have been copied successfully.")
#最后给到成功结果反馈

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值