Miten näen, onko tiedosto olemassa vai ei, käyttämättä try
-lausetta?
Sinulla on funktio os.path.exists
:
import os.path
os.path.exists(file_path)
Tämä palauttaa True
sekä tiedostoille että hakemistoille, mutta voit sen sijaan käyttää funktiota
os.path.isfile(file_path)
testataksesi, onko kyseessä nimenomaan tiedosto. Se seuraa symlinkkejä.
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