728x90
반응형
reads the file list.txt in which the file names are listed and deletes the rest leaving only the files that exist in it
import os
import glob
from tqdm import tqdm
import pdb
# Read the list of filenames from list.txt
for step in ["train","val", "test"]:
print("=="+step+"==")
with open(step+".txt", 'r') as f:
filenames = f.read().splitlines()
filenames = [f_n.split("/")[-1].rstrip("\n") for f_n in filenames]
# Get a list of all the files in the directory
imgs_in_dir = sorted(glob.glob(step+"/images/*"))
gts_in_dir = sorted(glob.glob(step+"/labels/*"))
# Loop through the files in the directory and delete any that aren't in the list
for img_f, gts_f in tqdm(zip(imgs_in_dir, gts_in_dir)):
file_nm = img_f.split("/")[-1].rstrip("\n")
if file_nm not in filenames:
# Check file names
if os.path.splitext(os.path.split(img_f)[-1])[0]!=os.path.splitext(os.path.split(gts_f)[-1])[0]:
pdb.set_trace()
print("Delete: %s, %s"%(img_f, gts_f))
os.remove(img_f)
os.remove(gts_f)
728x90
반응형
'사소한 Tip . 오류 해결법 > python' 카테고리의 다른 글
[Python/Google translator]구글 번역 API 파이썬 (0) | 2023.06.05 |
---|---|
[python] tuple element sum / 요소 합 (0) | 2023.02.16 |
Tuple 에서 index 찾기 (0) | 2022.12.27 |
[python/pdf2image] pdf 에서 페이지별 image 추출하기 (0) | 2022.11.23 |
[python] list 요소 2개씩 묶기 (0) | 2022.11.21 |