Ich wollte mal Fragen ob diese Funktion so ok ist ( Ich möchte in der Praxis oft das nach einer Exception etwas nochmal probiert wird ):
Oder hat jemand eine bessere Variante?
Code: Alles auswählen
def openfile(versuche):
while True:
try:
with open("test.txt", "r") as outfile:
inhalt = outfile.read()
except FileNotFoundError:
versuche -= 1
print(f"File konne nicht geöffnet werden es wird noch {versuche} mal versucht")
if versuche == 0:
raise
else:
return inhalt
def main():
inhalt = openfile(versuche=2)
print(inhalt)
if __name__ == "__main__":
main()