Created
September 20, 2021 11:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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