¿Cómo puedo ver si un archivo existe o no, sin utilizar la sentencia try
?
Tiene la función os.path.exists
:
import os.path
os.path.exists(file_path)
Esta función devuelve True
tanto para los archivos como para los directorios, pero puedes utilizar
os.path.isfile(file_path)
para probar si es un archivo específicamente. Sigue los enlaces simbólicos.
import os
os.path.exists(path) # Returns whether the path (directory or file) exists or not
os.path.isfile(path) # Returns whether the file exists or not