[英]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.