How to exit from Python without traceback?

0 votes
I would like to know how to I exit from Python without having an traceback dump on the output.

I still want want to be able to return an error code but I do not want to display the traceback log.

I want to be able to exit using exit(number) without trace but in case of an Exception (not an exit) I want the trace.
Jul 31, 2018 in Python by bug_seeker
• 15,300 points
256 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
Perhaps you're trying to catch all exceptions and this is catching the SystemExit exception raised by sys.exit()?

import sys try: sys.exit(1) # Or something that calls sys.exit() except SystemExit as e: sys.exit(e) except: # Cleanup and reraise. This will print a backtrace. # (Insert your cleanup code here.) raise

In general, using except: without naming an exception is a bad idea. You'll catch all kinds of stuff you don't want to catch -- like SystemExit -- and it can also mask your own programming errors. My example above is silly, unless you're doing something in terms of cleanup. You could replace it with:

import sys sys.exit(1) # Or something that calls sys.exit().

If you need to exit without raising SystemExit:

import os os._exit(1)

I do this, in code that runs under unittest and calls fork(). Unittest gets when the forked process raises SystemExit. This is definitely a corner case!
answered Jul 31, 2018 by Priyaj
• 56,140 points

Related Questions In Python

0 votes
1 answer

How to exit from Python without traceback?

shutil has many methods you can use. One ...READ MORE

answered May 10, 2018 in Python by charlie_brown
• 7,710 points
93 views
0 votes
1 answer

How to exit from Python without traceback?

You are presumably encountering an exception and ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 11,020 points
45 views
+2 votes
11 answers

How to print array/ list without brackets in python?

name=["a","b","c"] print (', '.join(name)) It will simply take all ...READ MORE

answered Apr 18, 2018 in Python by aayushi
• 750 points
14,491 views
0 votes
1 answer

How to get text label from SAP using pywinauto[python]

Hi. Can you please tell me what ...READ MORE

answered Jun 28, 2018 in Python by Nietzsche's daemon
• 4,260 points
239 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
63 views
+4 votes
6 answers
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 56,140 points
51 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 56,140 points
23 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.