Ako zistím, či súbor existuje alebo nie, bez použitia príkazu try
?
Máte funkciu os.path.exists
:
import os.path
os.path.exists(file_path)
Táto funkcia vracia True
pre súbory aj adresáre, ale namiesto toho môžete použiť
os.path.isfile(file_path)
na testovanie, či ide konkrétne o súbor. Sleduje symlinky.
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