Come posso vedere se un file esiste o no, senza usare l'istruzione try
?
Avete la funzione os.path.exists
:
import os.path
os.path.exists(file_path)
Questa restituisce True
sia per i file che per le directory, ma potete invece usare
os.path.isfile(file_path)
per testare se si tratta specificamente di un file. Segue i collegamenti simbolici.
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