I have several files inside a directory called imagex
, about 5,000 images.
A simple example of a file:
file_path = "C:/imagex/jonsnow.png"
How do I efficiently check if there is a specific image within this directory?
I have several files inside a directory called imagex
, about 5,000 images.
A simple example of a file:
file_path = "C:/imagex/jonsnow.png"
How do I efficiently check if there is a specific image within this directory?
You can do the following, using os.path.exists :
import os
file_path = "C:/imagex/jonsnow.png"
if os.path.exists(file_path):
# existe seja arquivo ou diretorio
To scan only files you can do:
...
if os.path.isfile(file_path):
# existe e e ficheiro
...