Check if there is a file inside a specific directory

Asked

Viewed 812 times

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 to step check, efficiently, if there is a specific image inside this directory?

  • Related and perhaps duplicated: http://answall.com/questions/133366/verifica-se-existe-arquivo-se-existi-report-python-blibioteca-os

  • @Miguel I guess I didn’t look hard enough, I hadn’t seen that question there.

  • Quiet, no problem, anyway the answer this goes straight to the question

1 answer

1


Here’s what you can do, using os.pathexists.:

import os

file_path = "C:/imagex/jonsnow.png"
if os.path.exists(file_path):
    # existe seja arquivo ou diretorio

To check only files you can do:

...
if os.path.isfile(file_path):
    # existe e e ficheiro
...
  • Funfou aqui! = ) Tks

  • You’re welcome @Acklay

Browser other questions tagged

You are not signed in. Login or sign up in order to post.