Search code examples
pythonpandasshellshutil

Copy images from one folder to another using their names on a pandas dataframe


I have a pandas dataframe that consists of 10000s of image names and these images are in a folder locally.

I want to filter that dataframe to pick certain images (in 1000s) and copy those images from the aformentioned local folder to another local folder.

Is there a way that it can be done in python?

I have tried to do that using glob but couldn't make much sense out of it.

I will create an sample example here: I have the following df:

img_name
2014.png 
2015.png 
2016.png 
2021.png 
2022.png 
2023.png  

I have a folder for ex. "my_images" and I wish to move "2015.png" and "2022.png" to another folder called "proc_images".

Thanks


Solution

  • import os
    import shutil
    
    path_to_your_files = '../my_images'
    copy_to_path = '../proc_images'
    
    files_list = sorted(os.listdir(path_to_your_files))
    file_names= ["2015.png","2022.png"]
    
    for curr_file in file_names:
        shutil.copyfile(os.path.join(path_to_your_files, curr_file),
                        os.path.join(copy_to_path, curr_file))  
    

    Something like this ?