Python实现相关文件的批量复制功能:一种简便的文件管理解决方案

本文介绍了一种使用Python的os和shutil模块解决点云任务中文件管理问题的方法,通过遍历图像文件夹并根据文件名自动查找和复制相应的.xyz文件到目标文件夹。
摘要由CSDN通过智能技术生成

问题由来:

在处理点云相关任务时,最终的结果我保存的是png图片,把问题结果对应的原始数据(.xyz文件)挑出来以便后续算法的该进。但是结果文件都是png图片,如果一个复制文件名去挑那也显得太sb了,想了想必须得实现自动化的文件复制移动。

文章内容概要:

本文介绍了如何使用 Python 中的 os 和 shutil 模块实现批量复制相关文件的功能。通过一个简单的代码示例,展示了如何遍历图像文件夹中的文件,并根据文件名查找相应的相关文件,然后将其复制到指定的目标文件夹中。

1. 提取图片的文件名

    # 检查目标文件夹是否存在,如果不存在则创建
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)
    
    # 获取图像文件夹下所有的 .png 文件
    image_files = [file for file in os.listdir(image_folder) if file.endswith('.png')]

2. 提取目标文件路径

    # 遍历所有 .png 文件
    for image_file in image_files:
        # 构建图像文件的路径
        image_path = os.path.join(image_folder, image_file)
        
        # 提取图像文件名(不包括扩展名)
        image_filename = os.path.splitext(image_file)[0]
        
        # 构建相关文件的路径
        related_file_path = os.path.join(file_folder, f"{image_filename}.{related_extension}")

3. 复制目标文件到新目录

        # 检查相关文件是否存在
        if os.path.exists(related_file_path):
            # 构建目标文件路径
            destination_path = os.path.join(destination_folder, f"{image_filename}.{related_extension}")
            
            # 复制相关文件到目标文件夹
            shutil.copyfile(related_file_path, destination_path)
            print(f"已复制相关文件: {related_file_path} -> {destination_path}")
        else:
            print(f"找不到相关文件: {related_file_path}")

4.完整代码

import os
import shutil

def copy_related_files(image_folder, file_folder, destination_folder, related_extension):
    # 检查目标文件夹是否存在,如果不存在则创建
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)
    
    # 获取图像文件夹下所有的 .png 文件
    image_files = [file for file in os.listdir(image_folder) if file.endswith('.png')]
    
    # 遍历所有 .png 文件
    for image_file in image_files:
        # 构建图像文件的路径
        image_path = os.path.join(image_folder, image_file)
        
        # 提取图像文件名(不包括扩展名)
        image_filename = os.path.splitext(image_file)[0]
        
        # 构建相关文件的路径
        related_file_path = os.path.join(file_folder, f"{image_filename}.{related_extension}")
        
        # 检查相关文件是否存在
        if os.path.exists(related_file_path):
            # 构建目标文件路径
            destination_path = os.path.join(destination_folder, f"{image_filename}.{related_extension}")
            
            # 复制相关文件到目标文件夹
            shutil.copyfile(related_file_path, destination_path)
            print(f"已复制相关文件: {related_file_path} -> {destination_path}")
        else:
            print(f"找不到相关文件: {related_file_path}")

# 指定图像文件夹、相关文件夹、目标文件夹和相关文件的扩展名
image_folder = r'C:\Users\DY\桌面\202401\result_20240429\analysis\bo'
file_folder = r'C:\Users\DY\桌面\202401'
destination_folder = r'C:\Users\DY\桌面\202401\analysis20240430'
related_extension = 'xyz'  # 相关文件的扩展名

# 调用函数复制相关文件
copy_related_files(image_folder, file_folder, destination_folder, related_extension)


运行结果

在这里插入图片描述
perfect!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值