程序目的:在一个文件下查找包含特定关键词的文件,如文件名中包含'CAT'的文件,并复制到指定文件夹下
import os
import shutil
src_dir_path = r'F://WTW dataset/train/train\images' # 源文件夹 修改1
to_dir_path = r'C://Users/PC/Desktop//图像'
key = '1' # 关键词 修改3
if os.path.exists(src_dir_path):
for file in os.listdir(src_dir_path):
logfile = open(file, 'r')
lines = logfile.readlines()
for line in lines:
if line.find(key) != -1:
shutil.copy(src_dir_path + '/' + file, to_dir_path + '/' + file)
结果报错:
File "F:\learn_pytorch\xml\1.py", line 14, in
logfile = open(file, 'r')
FileNotFoundError: [Errno 2] No such file or directory: '-a6BCtn-SHODim5RQbTWwQAAACMAAQED.jpg'