简体   繁体   English

如何在python的文件夹中保存同名文件?

[英]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.让两个文件夹命名为 A 和 B。A 包含 100 个文件,B 只有 80 个文件。 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.我只想保存 A 中与文件夹 C 中 B 具有相同对应关系的 80 个文件。

Here is a part of my code.这是我的代码的一部分。 However, it is throwing error :但是,它抛出错误:

Required argument 'img' (pos 2) not found.找不到必需的参数“img”(位置 2)。

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)) 

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;您的问题是您在尝试执行复制时没有将文件对象传递给cv2.imwrite(os.path.join(save_path,img_name)) that's what the error is telling you.这就是错误告诉你的。

However, your current approach includes a nested for loop which will give poor performance.但是,您当前的方法包括嵌套的for循环,这将导致性能不佳。 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.如果只想知道目录共有的文件,可以在每个目录中创建一set文件名,找到交集。 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).然后你只需要遍历公共文件并复制它们(如评论中所述,这里不需要cv2 - 它们可能是图像,但它们只是可以复制的常规文件)。

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):如果您从os.listdir的文件中os.listdir最后一个字符的原因是因为文件具有相同的名称但扩展名不同,则您只需要进行两个小的修改(在这里我假设扩展名是.png需要稍后添加回来):

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))

The any() method returns True if any element of an iterable is True.如果可迭代对象的任何元素为 True,则any()方法返回 True。 If not, any() returns False.如果不是,则 any() 返回 False。 shutil.copy - Copies the file src to the file or directory dst. shutil.copy - 将文件 src 复制到文件或目录 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 python 中文件名中具有相同字符串的文件夹中的文件? - How to access files in folder with same string in file name in python? 如何从输入目录文件夹输入并以与输入文件相同的名称将输出文件保存在python中的输出文件夹中 - How to input from input direcory folder and save output file in same name as input file in output folder in python 将文件保存到python中的文件夹 - Save files to a folder in python 如何将一堆同名文件复制到一个文件夹? - How to copy a bunch of files with same name to a folder? 如何完成此python函数以保存在同一个文件夹中? - How to complete this python function to save in the same folder? 如何将txt文件的文件夹另存为python中的变量 - How to save a folder of txt files as a variable in python 如何在Python中将文件保存到当前文件夹中的单独文件夹中? - How to save files to a separate folder in current folder in Python? 如何对文件名为python的文件夹中的文件进行排序 - How to sort files in a folder with file name in python 如何在 python 中保存具有相同 dataframe 名称的 dataframe? - How to save a dataframe with the same dataframe name in python? 如何检查目录中已存在的同名文件并使用python重命名新文件保存? - How do I check the existing file with same name in the directory and use python to rename new files to save?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM