Python copies files with the same name in another folder based on the names of a large number of files in a folder

This article   describes how to find a file with the same name as the file in this folder from another folder based on a large number of Excel table files in a folder based on the name of each file , and copy the found file with the same name to the method in the third folder .

  First, let's clarify the specific needs of this article. There is a folder in which there are a large number of Excel table files (in this article, we will take csvformat files as an example) - the files in this folder are actually the files that we traverse through a large number of table files through the article Python and filter out the tables Files with low data missing rate (https://blog.csdn.net/zhebushibiaoshifu/article/details/130714834) are selected; as shown in the figure below.

  In addition, we also have a folder (we call it a large folder ), which stores more Excel table files than the folder shown in the above picture ; what we hope to achieve is that from this large file folder , find the file with the same name as the Excel table file in the folder shown above , and copy the found file with the same name to another new folder.

  Knowing the requirements, we can start writing code. Among them, the code used in this article is as follows.

# -*- coding: utf-8 -*-
"""
Created on Tue May 16 22:32:00 2023

@author: fkxxgis
"""

import os
import shutil

def copy_file_with_name(source_path, target_path, new_path):
    source_file = os.listdir(source_path)
    
    for file in source_file:
        target_file_path = os.path.join(target_path, file)
        
        if os.path.isfile(target_file_path):
            new_file_path = os.path.join(new_path, file)
            shutil.copy(target_file_path, new_file_path)
            
copy_file_with_name("E:/01_Reflectivity/99_Model_Training/00_Data/02_Extract_Data/14_PointSelection/LowMissingRate",
                    "E:/01_Reflectivity/99_Model_Training/00_Data/02_Extract_Data/13_AllYearAverage",
                    "E:/01_Reflectivity/99_Model_Training/00_Data/02_Extract_Data/15_8DaysSynthesis")

  In the above code, we first import the osand shutilmodule, oswhich is used to handle file and folder operations, and shutilthe module is used to perform file copy operations. Then define a copy_file_with_namefunction called , which accepts 3a parameter: source_paththe path of the source folder, the path target_pathof the target folder (that is, the large folder mentioned above ), new_pathand the path of the new folder. source_file = os.listdir(source_path)Get all files in the source folder and assign the file list to source_file.

  In this next loop, for each file in the source folder, we construct the full path to the target file target_file_path, where target_pathis the path to the target folder and fileis the name of the file in the source folder. Then use os.path.isfile(target_file_path)to check whether the target file exists, and if so, proceed to the next step. Next, we construct the full path to the new file new_file_path, where new_pathis the path to the new folder and fileis the name of the file in the source folder. Finally, use shutil.copya function to copy the target files into the new folder.

  The last line of code calls copy_file_with_namethe function, passing in the paths of three folders as parameters, and copies files from one folder to another.

  After running the above code, we can new_pathfind the copied files in this folder; among them, it is obvious that new_paththe number of files in this folder source_pathis the same as the number of files in the folder.

  So far, you're done.

Welcome to pay attention: Crazy learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/130715870