Kitty
Kitty

Reputation: 21

How to save files with same name in folder in python?

I have two folders with images. Let the two folder names A and B. A contains 100 files and B has only 80 files. Both the files have the same name. I want to save only the 80 files from A which has the same correspondence to B in folder C.

Here is a part of my code. However, it is throwing error :

Required argument 'img' (pos 2) not found.

path1= '/home/vplab/Kitty/Saliency Dataset/PiCANet-Implementation/TrainSet/images'
path_mask= '/home/vplab/Kitty/Saliency Dataset/PiCANet-Implementation/TrainSet/masks'
save_path = '/home/vplab/Kitty/Saliency Dataset/PiCANet-Implementation/TrainSet/exp'
for file in os.listdir(path1):
    for file1 in os.listdir(path_mask):
        img_name = file[:-4]
        mask_name =file1[:-4]
        if img_name == mask_name:
            cv2.imwrite(os.path.join(save_path,img_name)) 

Upvotes: 0

Views: 3704

Answers (2)

roganjosh
roganjosh

Reputation: 13175

Your issue here is that you are not passing a file object to cv2.imwrite(os.path.join(save_path,img_name)) when trying to perform the copy; that's what the error is telling you.

However, your current approach includes a nested for loop which will give poor performance. If you only want to know the files that the directories have in common, you can create a set of the file names in each directory and find the intersection. Then you just need to iterate through the common files and copy them over (as said in the comments, there's no need for cv2 here - they may be images but they're just regular files that can be copied).

import os
from shutil import copyfile

dir_1 = 'A'
dir_2 = 'B'
output_dir = 'C'

files_1 = os.listdir(dir_1)
files_2 = os.listdir(dir_2)

# Find the common files between both
common_files = set(files_1).intersection(files_2)

# Copy the common files over.
for file in common_files:
    copyfile(os.path.join(dir_1, file),
             os.path.join(output_dir, file)) 

If the reason that you are stripping the last characters from the files in os.listdir is because the files have the same name but different extensions, you only need to make two small modifications (where here I'm assuming the extension is .png that needs to be added back later):

files_1 = [item[:-4] for item in os.listdir(dir_1)]
files_2 = [item[:-4] for item in os.listdir(dir_2)]

And:

for file in common_files:
    file = file + '.png' # Add the extension back on to the file name
    copyfile(os.path.join(dir_1, file),
             os.path.join(output_dir, file))

Upvotes: 3

bharatk
bharatk

Reputation: 4315

The any() method returns True if any element of an iterable is True. If not, any() returns False. shutil.copy - Copies the file src to the file or directory dst.

import os
import shutil


def read_file(folderName,folderPath):
    ''' Return list of files name '''
    path = folderPath+folderName
    return [file for file in os.listdir(path)]

def save_file(soureFolderName,destFolderName,folderPath,fileName):
    ''' Save file on destination folder'''
    try:
        source_path = folderPath+soureFolderName+"/"+fileName
        dest_path = folderPath+destFolderName+"/"+fileName
        shutil.copy(source_path, dest_path)
    except Exception as e:
        print(e)

base_path = '/home/vplab/Kitty/Saliency Dataset/PiCANet-Implementation/TrainSet/'
folder_images_files = read_file('images',base_path)
folder_masks_file = read_file('masks',base_path)

for file_1 in folder_images_files:
    #Check folder A file is exists in folder B
    if any(file_1 == file_2 for file_2 in folder_masks_file):
        save_file("images","exp",base_path,file_1)

Upvotes: 0

Related Questions