Kaip patikrinti, ar failas egzistuoja, ar ne, nenaudojant try
sakinio?
Turite funkciją os.path.exists
:
import os.path
os.path.exists(file_path)
Ši funkcija grąžina True
ir failams, ir katalogams, bet vietoj to galite naudoti
os.path.isfile(file_path)
norėdami patikrinti, ar tai konkrečiai failas. Jis seka simines nuorodas.
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