Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:55:03+00:00 2026-06-06T23:55:03+00:00

import os, sys from cStringIO import StringIO import traceback old_stdErr = sys.stderr sys.stderr =

  • 0
import os, sys
from cStringIO import StringIO
import traceback

old_stdErr = sys.stderr
sys.stderr = mystdErr = StringIO()

try:
    p = 100/0
except Exception:
     traceback.print_exc(file=sys.stderr)

print mystdErr.getvalue()             ### -- 1st Print statement 

try:
   q = 100/0
except Exception:
   traceback.print_exc(file = sys.stderr)

print mystdErr.getvalue()             ### -- 2nd Print statement

This piece of code .. What the output will be

Output from the 1st print statement

Traceback (most recent call last):
File “C:\Users\manojtut\Desktop\untitled-1.py”, line 9, in
p = 100/0
ZeroDivisionError: integer division or modulo by zero

Output from the 2nd print statement

Traceback (most recent call last):
File “C:\Users\manojtut\Desktop\untitled-1.py”, line 9, in
p = 100/0
ZeroDivisionError: integer division or modulo by zero
Traceback (most recent call last):
File “C:\Users\manojtut\Desktop\untitled-1.py”, line 16, in
q = 100/0
ZeroDivisionError: integer division or modulo by zero

As you can see , the exception encountered at 1st zerodivision is being printed again in the second print statement which I don’t want. Can I somehow eliminate/flush away the first exception traceback so that only the second exception’s traceback can be printed in the second print statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:55:04+00:00Added an answer on June 6, 2026 at 11:55 pm

    Add this:

    mystdErr.reset()
    mystdErr.truncate()
    

    before the second block. reset resets the file position to the beginning and writing begins from there. truncate removes all data after the current position. If ‘truncate’ is omitted – the data in the stream will be overwritten from subsequent writes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a main.py file inside /home/richard/projects/hello-python directory: import sys sys.path.append('/home/richard/projects/hello-python') from Encode
import MySQLdb import sys from libdesklets.controls import Control from IDBConnection import IDBConnection class DBConnection(Control,
for this code: import sys import gevent from gevent import monkey monkey.patch_all() import requests
my_var = 3 assert('my_var' in locals()) import a_module assert('a_module' in locals()) from sys import
import sys sys.path.append('/home/myuser/svn-repos/myproject') from myproject.settings import * But, it says module not found when
This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/ import sys import random if len(sys.argv) != 4: print
I wrote a program called Hello.py that looks like this: import pygame, sys from
import sys def checkarg(): try: filename=str(sys.argv[1]) if filename==-mycommand: print SPECIFIC_TEXT sys.exit() else: return filename
I'm trying to translate code from this thread in python: import sys from PyQt4.QtCore
I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.