Kuidas näha, kas fail on olemas või mitte, ilma try
avaldist kasutamata?
Teil on funktsioon os.path.exists
:
import os.path
os.path.exists(file_path)
See tagastab True
nii failide kui ka kataloogide puhul, kuid selle asemel võite kasutada True
.
os.path.isfile(file_path)
testida, kas tegemist on failiga. See järgib sümbolilinkke.
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