Python Forum

Full Version: Try Except Else problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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...")
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