# -*- coding: utf-8 -*-
import os
import shutil
# 创建一个txt文件,文件名为mytxtfile
def text_create(full_path):
with open(full_path,"a") as file: #”w"代表着每次运行都覆盖内容
file.write('')
root_path = 'F:\\project\\ship_DataSet\\NWPU_VHR-10\\ground_truth' #待读取文件的文件夹绝对地址
gt_old_path =root_path
gt_new_path='F:\\project\\ship_DataSet\\NWPU_VHR-10\\after_search\\gt'
im_old_path='F:\\project\\ship_DataSet\\NWPU_VHR-10\\positive_image_set'
im_new_path ='F:\\project\\ship_DataSet\\NWPU_VHR-10\\after_search\\image'
files = os.listdir(root_path) #获得文件夹中所有文件的名称列表
searchfile='F:\\project\\ship_DataSet\\NWPU_VHR-10\\after_search\\searchfile.txt'
for path in files:
#逐个打开路径下文件
file = open(root_path+'\\'+path)
#获取去除后缀的文件名
index = path.rfind('.')
path_name = path[:index]
# print(path_name)
#筛选txt中包含特定字符串的txt,并另存
for line in file.readlines():
key = '),2'
if key in line:
#写入符合查找规则的txt文件名称
with open(searchfile,"a") as file: #”w"代表着每次运行都覆盖内容 "a"追加
file.write(path_name+'\n')
# print(path)
# 新建txt才能进行复制
gt_new_path_full=gt_new_path+'\\'+path#新txt文件
text_create(gt_new_path_full) # 新建txt
gt_old_path_full=gt_old_path+'\\'+path#旧txt文件
shutil.copyfile(gt_old_path_full,gt_new_path_full)#复制txt
# 新建jpg才能进行复制
im_new_path_full=im_new_path+'\\'+path_name+'.jpg'#新txt文件
text_create(im_new_path_full) # 新建jpg
im_old_path_full=im_old_path+'\\'+path_name+'.jpg'#旧txt文件
shutil.copyfile(im_old_path_full,im_new_path_full)#复制txt
break
#
python 提取符合筛选条件的文件并复制 读取文件夹下的所有文件 创建txt文件 文件读写(追加、覆盖) 文件复制 筛选含有某字符串的txt
最新推荐文章于 2023-09-01 01:55:12 发布