Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try Except Else problem
#1
Hello everyone , i have a problem with my block try , when there is no error it will ignore the else part. anyone who could explain to me why? thanks

fichiera = input("entré un nom de fichier")
try:
    fichiersauv = open("fichiera","rb")
    fichiersauv.close()
except FileNotFoundError:
    fichiersauv = open(fichiera, "wb")
    pickle.dump(newpickle, fichiersauv)
    fichiersauv.close()
else:
    print("le fichier existe déjà, voulez vous l'écraser? OUI / NON")
    sauvegarder = input()
    while sauvegarder != "OUI" and sauvegarder != "NON":
        print("Veuillez répondre par OUI ou par NON ")
        sauvegarder = input()
    if sauvegarder == "OUI":
        fichiersauv = open(fichiera, "wb")
        pickle.dump(newpickle, fichiersauv)
        fichiersauv.close()
    else:
        print("Retour au menu...")
Reply
#2
Are u sure there is no exception happened? give some running results.

I think I found your problem.
You first sentense fichiersauv = open("fichiera","rb") , there should be no quotation mark.

Besides , if this is your whole code fragment , I think u should import the pickle module
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020