2 years ago
#71166
watchtower
Try and except exit error after executing the except line
I am trying to exit out of the program after executing the except line. I have tried sys.exit(1)
but it is not working. I am opening an .txt file and and passing it through the code. The code works perfectly. I am just trying to exit out of the code if the wrong file is input like temp file rather then temp.txt.
# Ask input from the user
fname = input("Enter File Name: ")
# Open the file and check if entry is valid
try:
temp_file = open(fname)
except:
print("File cannot be opened:", fname)
# I NEED YOUR HELP HERE
quit()
count = 0
total = 0
for temp in temp_file:
# Remove white space
temp = temp.strip()
# Find the exact string
if temp.startswith("X-DSPAM-Confidence:"):
# Find the position of the number and convert to float
pos = temp.find(":")
float_pos = float(temp[pos+1:])
# Add the numbers and count the number of lines
total += float_pos
count += 1
# Print out the average
print("Average spam confidence: ", total/count)
try-catch
exit
except
0 Answers
Your Answer