Check if a file exists within a specific directory

1

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?

    
asked by anonymous 23.03.2017 / 16:33

1 answer

1

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
...
    
23.03.2017 / 16:35