I need to check the current directory and see if a file with an extension exists. My setup will (usually) only have one file with this extension. I need to check if that file exists, and if it does, run a command. However, it runs the else
multiple times because there are multiple files with alternate extensions. It must only run the else
if the file does not exist, not once for every other file. My code sample is below.
Thanks in advance.
The directory is structured like so:
dir_________________________________________
\ \ \ \
file.false file.false file.true file.false
Whan I run:
import os
for File in os.listdir("."):
if File.endswith(".true"):
print("true")
else:
print("false")
The output is:
false
false
true
false
The issue with this is if I replaced print("false")
with something useful, it will run it multiple times.
Aucun commentaire:
Enregistrer un commentaire