• Register
0 votes
1.1k views

Problem :

I have attempted to reproduce my problem described below under only Python 2.7.3.

The most reliable way that I found to elicit my problem in the question is to pipe my output of the next test script through the use of  : (under bash):

try:
    for m in range(40):
        print m
except:
    pass

The error message received as below :

% mypython testscript.py | :
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

As the test script clearly shows that the error cannot be trapped with the try-except.

My question is as below:

How can I modify my test script above to avoid facing the error message whenmy script is run as shown above (under  the Unix/bash)?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

When you try to pipe the output of one program into the another, the output produced by the first  writing program gets backed up in the buffer, and it waits for your reading program to request that data from your buffer. As long as your buffer is nonempty, any attempt to made to close your writing file object is supposed to fail with the error. This is the root cause behind the messages you are expearencing.

The specific code that is triggering the error is in your C language implementation of the Python, which explains why you can not catch the error with the try/except block: as it runs after your contents of the script has finished the processing.

9 7 4
38,600 points

Related questions

0 votes
0 answers 30 views
30 views
I saw this code to find difference between diagonal sums of a square matrix. #!/bin/python3 import math import os import random import re import sys def diagonalDifference(arr): d1=0 d2=0 a=n-1 print(arr[1][1]) for i in range(0,n): d1+=arr[i][i] ... ['OUTPUT_PATH'], 'w') with fptr = sys.stdout, the output shows no difference. What then is the difference between functions of these two statements?
asked Oct 29, 2020 psandprop 2.4k points
0 votes
1 answer 49 views
0 votes
1 answer 212 views
212 views
Problem : I am trying to develop some selenium tests which should run with headless firefox but the following error suddenly popped up in my server.log file when I was trying to connect to the webdriver via following command: driver = webdriver.Firefox(firefox_profile = profile, log_path = ... spec as below: python 2.7.12, Firefox: Mozilla Firefox 57.0.3, geckodriver: 0.19.1, selenium: 3.8.0
asked Dec 3, 2019 alecxe 7.5k points
0 votes
1 answer 1.9K views
1.9K views
Problem : The import matplotlib.pyplot as plt is is giving me the error with python2.7, anaconda3. The error shown is as below: ImportError: libGL.so.1: cannot open shared object file: No such file or directory The Detailed Error report is as below : Traceback ... <module> from PyQt5 import QtCore, QtGui, QtWidgets ImportError: libGL.so.1: cannot open shared object file: No such file or directory
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 754 views
754 views
Problem : While trying to import the OpenCV, using import cv2 I facing the below error: /usr/local/lib/python2.7/dist-packages/cv2/__init__.py in <module>() 12 13 # make IDE's (PyCharm) autocompletion happy ----> 14 from .cv2 import * 15 16 # wildcard import ... - I am trying to play around with the Google's new Colaboratory tool. Please let me know if someone have solution for the above issue.
asked Dec 18, 2019 alecxe 7.5k points
0 votes
1 answer 1.5K views
1.5K views
Problem : I am facing following error : ImportError: libcuda.so.1: cannot open shared object file: No such file or directory Failed to load the native TensorFlow runtime. Above error comes up when I tried to import the tensorflow. Is there anybody who has faced this error before and know the solution on it?
asked Dec 10, 2019 alecxe 7.5k points
1 vote
1 answer 72 views
72 views
Problem: I am a newbee in programmingand I am doing a online course on python. To follow that course I wrote a code: def add(x , y): return x + y def del(x, y): return x - y choice = int(input("Please enter your Choice: ")) print 1.For addition print ... error like: File "calc.py", line 7 print 1.For addition ^ SyntaxError: Missing parentheses in call to 'print' What should I do now please help
asked Apr 4, 2020 LizzyM 6.1k points
0 votes
1 answer 35 views
35 views
I am new to pyhton programming . I was doing some coding practice. I wrote a code: def addition(a , b): return a + b def delete(a, b): return a - b choice = int(input("Please enter your Choice: ")) print 1.For addition print 2.For delete but it gives me some error like: File "main.py", line 7 print 1.For addition ^ SyntaxError waht is the problem with my code can anyone help?
asked Mar 15, 2020 LizzyM 6.1k points