Skip to content

Instantly share code, notes, and snippets.

@igarag
Created September 20, 2021 11:54
Show Gist options
  • Save igarag/5be8e7bb440f29f1804703e147ab283b to your computer and use it in GitHub Desktop.
Save igarag/5be8e7bb440f29f1804703e147ab283b to your computer and use it in GitHub Desktop.
Remove from the image folder any image that is not in the labels folder.
"""
Source: https://unix.stackexchange.com/questions/528490/python-removing-jpg-files-without-matching-txt-files
Remove from the image folder any image that is not in the labels folder.
It is recommended to make a previous raw folder for security. In the opposite
case (more labels than images) rename the necessary directories and variables.
.
├── images
│ ├── 1.jpg
│ ├── 2.jpg
│ ├── 3.jpg
│ ├── 4.jpg
│ └── 5.jpg
└── labels
├── 1.txt
├── 2.txt
└── 5.txt
"""
from os import listdir, remove
labels = listdir('labels') # *.txt
images = listdir('images') # *.jpg
for image in images:
if '{}.{}'.format(image.split('.')[0], 'txt') not in labels:
print(f'Going to remove {image}')
remove(f'images/{image}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment